Struts 2 Control Tags Example Tutorial

We learned about Struts 2 Data tags in the last post, today we will look into the control tags provided by Struts 2 framework.

Control tags are used for manipulation and navigation of data from a collection. Some of the important Struts 2 control tags are:

  1. if-elseif-else tags: These are the basic control tags used for conditional logic in result pages. elseif and else tags always work with if tags. if and elseif tags have test attribute where we can provide expression that should result in boolean values, true or false. Example usage is:

    else tag doesn’t have any attribute.
  2. iterator tag: We can use iterator tag to loop through a collection of objects. The collection could be array, list, map etc. iterator tags provide option to define a variable in the ActionContext with status attribute. IteratorStatus provides the current status of iterator and expose some methods to get count, odd or even, first or last element etc. Example usage is:

    For iterating a map, we need to provide property tag value as key and value.

    The other control tags are provided to work with iterators.
  3. append tag: This tag can be used to append multiple lists and then use them in iterator. The lists to append are provided with param tag and lists are appended one after another. So first list elements will be retrieved first by iterator then second list elements and so on. Example usage is:
  4. merge tag: We can use merge tag to merge multiple lists. The first elements from all the lists are merged first, then second element and so on. We can use merge tag like below.
  5. sort tag: We can use sort tag to get the sorted list. We need to provide a comparator to be used for sorting the list. We can use it like below.
  6. subset tag: We can use subset tag to get the subset of an iterator. We need to provide a Decider implementation that will be used by subset tag to filter elements as whether to include them in iterator or not. We can use it like below.
  7. generator tag: We can use generator tag to create an iterator from given values. We can provide separator to use the delimiter for the values and we can also provide Converter implementation to convert the String to an Object. We can use it like below.

Let’s see control tags in action with a simple struts 2 project. Our project structure will look like below image.

struts2-control-tags-project

Configuration Files

web.xml

pom.xml

struts.xml

Configuration files are self explanatory and used to configure Struts2 framework and action configuration for our example.

Java Bean

A simple java bean class that will be used in Action class to set some values and then in JSP page to showcase the struts2 control tags usage.

Comparator Class

A simple Comparator implementation that will be used in result page sort tag example. We can use Comparator to sort a list of custom objects also, please read more at Java Comparator Example.

Action Class

A simple action class implementation where we are populating Employee bean variables to be used in result page. The important point to note is getCountryDecider() method that will be used in result page subset tag example to get the subset iterator.

Result Page

home.jsp

This is the result page where we are using Struts2 control tags to manipulate data and collections. Please look into the usage of different tags carefully for better understanding.

When we invoke home action in browser, we get following response page.

Download the complete project from above link and play around with different attributes of the control tags to learn more about them.

That’s all for Struts2 Control tags example tutorial, I hope you liked it.

By admin

Leave a Reply

%d bloggers like this: