Skip to main content

Struts 2 Tags

Struts 2 Tags

<%@taglib prefix="s" uri="/struts-tags"%>

<h3></h3>

<h3> Conditionals </h3>

<s:if test='name=="abc"'>

you are <s:property value="name"/>

</s:if>

<s:else>

you are not abc

</s:else>





<h3>collections and Iteration(ArrayList) </h3>

<ul>

    <s:iterator value="books" var="book">

    <li><s:property value="book"/></li>

    </s:iterator>

</ul>



<h3>collections and Iteration (Map)</h3>

<ul>

    <s:iterator value="emails">

    <li><s:property value="key"/> : <s:property value="value"/></li>

    </s:iterator>

</ul>



<ul>

    <s:iterator value="emails" var="item">

    <li><s:property value="#item.key"/> : <s:property value="#item.value"/></li>

    </s:iterator>

</ul>



<h3>tracking iteration status </h3>

<ul>

    <s:iterator value="books" var="book" status="stat">

    

    <li>

        

        <s:property value="#stat.index"/>:

        <s:property value="#stat.count"/>:

        <s:property value="#stat.even"/>:

        <s:property value="#stat.odd"/>:

        <s:property value="#stat.first"/>:

        <s:property value="#stat.last"/>:

        <s:property value="#stat.modulus(2)"/>:

        <s:property value="#stat.modulus(4)"/>:

        <s:property value="#stat.count%4"/>:

        <s:property value="book"/>

    </li>

    </s:iterator>

</ul>





<h3> s:generator : it is used to create a list in value stack</h3>

<s:generator val="'1,2,3,4,5'" separator=",">

    <ul>

        <s:iterator>

        <li><s:property/></li>

        </s:iterator>

    </ul>

</s:generator>



<h3> here we can create a variavle that poits the list generated by generator</h3>

<s:generator val="'1,2,3,4,5'" separator="," var="mylist"/>

<h3> now we can use it anywhere</h3>

<ul>

    <s:iterator value="mylist">

    <li><s:property/></li>

    </s:iterator>

</ul>

<h3> After full iteration we can not use mylist itarator again

we need to generate new list</h3>



<h3> Using convertor in generator.

get a object from action class that implements IteratorGenerator.Converter </h3>

<s:generator val="'1,2,3,4,5,6,7,8,9,0'" separator="," var="mylist" converter="digitConverter"/>

<ul>

    <s:iterator value="mylist">

    <li><s:property/></li>

    </s:iterator>

</ul>



<h3>s:append </h3>

<s:append var="appended">

    <s:param value="{1,2,3,4}"/>

    <s:param value="{'a','b','c','d'}"/>

</s:append>



<s:iterator value="appended">

    <s:property/> 

</s:iterator>



<h3>s:merge</h3>



<s:merge var="merged">

    <s:param value="{1,2,3,4}"/>

    <s:param value="{'a','b','c','d'}"/>

</s:merge>



<s:iterator value="merged">

    <s:property/> 

</s:iterator>



<h3>s:subset - use to get iterate on portion of items from collection</h3>

<!--data from books property of action -->

<s:subset source='books' start="2" count="2">

    <s:iterator>

        <s:property/> 

    </s:iterator>

</s:subset>

<%--<s:debug/>--%>





Output

Conditionals

you are not abc

collections and Iteration(ArrayList)

  • Java
  • J2EE
  • JSP
  • Servlet
  • C
  • C++

collections and Iteration (Map)

  • abc : abc@gmail.com
  • xyz : xyz@yahoo.com
  • bbc : bbc@gjh.com
  • abc : abc@gmail.com
  • xyz : xyz@yahoo.com
  • bbc : bbc@gjh.com

tracking iteration status

  • 0: 1: false: true: true: false: 1: 1: 1: Java
  • 1: 2: true: false: false: false: 0: 2: 2: J2EE
  • 2: 3: false: true: false: false: 1: 3: 3: JSP
  • 3: 4: true: false: false: false: 0: 0: 0: Servlet
  • 4: 5: false: true: false: false: 1: 1: 1: C
  • 5: 6: true: false: false: true: 0: 2: 2: C++

s:generator : it is used to create a list in value stack

  • 1
  • 2
  • 3
  • 4
  • 5

here we can create a variavle that poits the list generated by generator

now we can use it anywhere

  • 1
  • 2
  • 3
  • 4
  • 5

After full iteration we can not use mylist itarator again we need to generate new list

Using convertor in generator. get a object from action class that implements IteratorGenerator.Converter

  • one
  • two
  • three
  • four
  • five
  • six
  • seven
  • eight
  • nine
  • zero

s:append

1  2  3  4  a  b  c  d 

s:merge

1  a  2  b  3  c  4  d 

s:subset - use to get iterate on portion of items from collection

JSP  Servlet

Comments

Popular posts from this blog

Using HyperSQL (HSQLDB)

HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very a small database that supports almost all features of the standard database system. It comes with small jar file that can be found in lib folder. The HSQLDB jar package is located in the /lib directory of the ZIP package and contains several components and programs. Core components of jar file are : HyperSQL RDBMS Engine (HSQLDB), HyperSQL JDBC Driver, Database Manager, and Sql Tool. Installing and Using Download: download latest release of HyperSQL database from http://hsqldb.org website and extract it. You will see following contents. Here "bin" directory contains some batch files those can be used to run a swing based GUI tool. You can use runManagerSwing.bat to connect to database, but database must be on before running it. Directory lib contains File hsqldb.jar . It is the database to be used by you. Running database First

How to handle values from dynamically generated elements in web page using struts2

Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values for unknown number of repeating for some information. for example when you are asking to get the projects details from user, you need to put the option to add the more project for the user since you don't known how many projects user have. In the HTML form, you repeat the particular section to get the multiple values for those elements. In Html page , you can put the option to add new row of elements or text fields by writing the java script or using JQuery API. Now, the question is that how to capture the values of dynamically generated text fields on the server. Using the servlet programming you can get the values by using getParameters() method that resultants the array of the parameter having the same name. But this limit you to naming the text fields in the HTML form. To ally this approach, you have to take the same name for t

In Process Mode of HSQLDB in web application.

If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLDB into your web application and it runs as a part of your web application programm in the same JVM. In this mode, the database does not open any port to connect to the application on the hosing machine and you don't need to configure anything to access it. Database is not expposed to other application and can not be accessed from any dabase tools like dbVisualizer etc. In this mode ,database will be unknown from any other person except you. But in the 1.8.0 version, you can use Server intance for external as well as in process access.  To close the databse, you can issue SHUTDOWN command as an SQL query.   In the in-process mode, database starts from JDBC with the associated databse file provided through  connection URL. for example   DriverManager.getConnection("jdbc:hsqldb:mydatabase","SA","");   Here myd