Skip to main content

Posts

Showing posts from July, 2011

Struts2 - Subset Tag (Control Tags)

In this section, we are going to describe the subset tag. The subset tag is a generic tag that takes an iterator and outputs a subset of it. It delegates to org.apache.struts2.util.SubsetIteratorFilter internally to perform the subset functionality. Add the following code snippet into the struts.xml file. struts.xml <action name="subsetTag" class="net.struts2.mergeTag"> <result>/pages/genericTags/subsetTag.jsp</result>  </action> Create a list in the action class and populate it with various items as shown in the " subsetTag " class. subsetTag.java package net.struts2;  import com.opensymphony.xwork2.ActionSupport;  import java.util.*; public class subsetTag extends ActionSupport {     private List myList;    public String execute() throws Exception{        myList...

Struts2 - Merge Tag (Control Tags)

In this section, we are going to describe the merge tag. The merge tag is a generic tag that is used to merge iterators. The successive call to the merge iterator causes each merge iterator to have a chance to expose its element, subsequently next call allows the next iterator to expose its element. Once the last iterator is done exposing its element, the first iterator is allowed to do so again (unless it is exhausted of entries). In the current example, 2 lists being merged, each list have 5 entries, the following will be the logic. 1. Display first element of the first list. 2. Display first element of the second list. 3. Display second element of the first list. 4. Display second element of the second list. 5. Display third element of the first list. 6. Display thrid element of the second list.....and so on. Add the following code snippet into the struts.xml file. struts.xml <action name="mergeTag" class="net.struts2.mergeTag"> ...

Struts2 - Iterator Tag (Control Tags)

In this section, we are going to describe the Iterator tag. Iterator tag is used to iterate over a value. An iterable value can be either of: java.util.Collection, java.util.Iterator. Add the following code snippet into the struts.xml file. struts.xml <action name="iteratorTag" class="net.struts2.iteratorTag"> <result>/pages/genericTags/iteratorTag.jsp</result>  </action> Create an action class as shown: iteratorTag.java package net.struts2;  import com.opensymphony.xwork2.ActionSupport;  import java.util.*; public class iteratorTag extends ActionSupport{  private List myList;  public String execute()throws Exception{     myList = new ArrayList();      myList.add("Fruits");      myList.add("Apple");     ...

Strits2 - Generator Tag (Control Tags)

In this section, we are going to describe the generator tag. The generator tag is a generic tag that is used to generate iterators based on different attributes passed. Here we will not pass any attribute. Add the following code snippet into the struts.xml file. sturts.xml <action name="GeneratorTag" class="net.struts2.GeneratorTag"> <result>/pages/genericTags/GeneratorTag.jsp</result>  </action> Create an action class as shown: GeneratorTag.java package net.struts2;  import com.opensymphony.xwork2.ActionSupport;  import org.apache.struts2.util.IteratorGenerator.Converter; public class GeneratorTag extends ActionSupport {    public String excute() throws Exception{   return SUCCESS;  } } Create a jsp page where the generator tag generates a simple iterator based on the val attribute supplied and tag prints...

Struts2 - Append Tag (Control Tags)

In this section, we are going to describe the append tag. The append tag is a generic tag that is used to merge multiple iterators into one iterator. Append Iterator Tag is used to append iterators to form an appended iterator through which the entries goes from one iterator to another after each respective iterator is exhausted of entries. Add the following code snippet into the struts.xml file. struts.xml: <action name="AppendTag" class="net.struts.AppendTag"> <result>/pages/genericTags/AppendTag.jsp</result>  </action> Create two lists in the action class and populate them with various items as shown in the " AppendTag " class. AppendTag.java package net.stuts2-example;  import com.opensymphony.xwork2.ActionSupport;  import java.util.*; public class AppendTag extends ActionSupport{   private List myList;  private List myList1; ...

Struts2 - Control Tags

In this section we are going to discuss the various control tags ( The Control Tags are used for flow control such as if, else and iterate.) 'If' tag could be used by itself or with 'Else If' Tag and/or single/multiple 'Else' Tag. Create a JSP page IfControlTag.jsp. Set a property 'technologyName' with a value 'Java' as <s:set name="technologyName" value="%{'Java'}"/> Among if, elseif and else tags only one tag evaluates at a time. Evaluation is based upon the conditions being processed. Evaluated conditions must be of Boolean type. This is illustrated in the following Jsp page. [Note:If the condition in tag evaluates to 'true' then only this tag is evaluated and others are discarded. As illustrated in the example. If the condition in tag evaluates to 'false' and tag evaluates to 'true' then the body of the tag is processed. If the condition in tag and tags evaluates to ...