Meyyappan has posted 101 posts at DZone. View Full User Profile

Struts Logic Tag Tutorial

06.14.2012
| 46943 views |
  • submit to reddit

In this example you will learn how to use Struts Logic Tags. In order to use the Struts Logic Tag you need to include the following taglib directive in the jsp page.

<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

To use the Struts Logic Tag Library you need to add the following <taglib> subelement to the web.xml file.

<jsp-config>
    <taglib>
        <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
        <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
    </taglib>
</jsp-config>

In this example we will create a user.jsp page wich displays the user details according to the conditions specified using the logic tags. The UserForm has the following attributes and the corresponding getter and setter methods.

private String name;
private int age;
private float height;
private float weight;
private String favouriteFood;
private ArrayList hobbies;

Initially we set default values for these attributes in the UserAction.

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception {
    ArrayList hobbiesList = new ArrayList();
    UserForm userForm = (UserForm)form;
    userForm.setName("Eswar");
    userForm.setAge(21);
    userForm.setHeight(5.11f);
    userForm.setWeight(70f);
    userForm.setFavouriteFood("Fish and Chicken");
    hobbiesList.add("Music");
    hobbiesList.add("Art");
    hobbiesList.add("Dance");
    userForm.setHobbies(hobbiesList);
    return mapping.findForward(SUCCESS);
}

Based on the conditions specified using the logic tags in the jsp page, the corresponding messages will be displayed to the user.

<logic:present /> <logic:notPresent />

The following code will dispaly "The User Name is Eswar." if the name attribute has some value else it will display "The User is not Present.
<logic:present name="UserForm" property="name">
The User Name is Eswar.
</logic:present>
<logic:notPresent name="UserForm" property="name">
The User is not Present.
</logic:notPresent>

<logic:equal /> <logic:notEqual />

The following code will dispaly "The age is equal to 18." if the value of the age attribute is 18 else it will dispaly "The age is not equal to 18."

<logic:equal name="UserForm" property="age" value="18">
The age is equal to 18.
</logic:equal>
<logic:notEqual name="UserForm" property="age" value="18">
The age is not equal to 18.
</logic:notEqual>

<logic:greaterEqual /> <logic:greaterThan />

The following code will dispaly "The height is greater than or equal to 5.11" if the value of the height attribute is equal to or greater than 5.11 else it will display "The height is greater than 5.11" if the value of the height attribute is greater than 5.11.

<logic:greaterEqual name="UserForm" property="height" value="5.11">
The height is greater than or equal to 5.11
</logic:greaterEqual>
<logic:greaterThan name="UserForm" property="height" value="5.11">
The height is greater than 5.11
</logic:greaterThan>

<logic:lessEqual /> <logic:lessThan />

The following code will dispaly "The weight is less than or equal to 70." if the value of the weight attribute is less than or equal to 70 else it will display "The weight is less than 70" if the value of the weight attribute is less than 70.

<logic:lessEqual name="UserForm" property="weight" value="70">
The weight is less than or equal to 70.
</logic:lessEqual>
<logic:lessThan name="UserForm" property="weight" value="70">
The weight is less than 70.
</logic:lessThan>

<logic:match /> <logic:notMatch />

The following code will display "The user's favourite food includes Chicken" if the value of the favouriteFood attribute contains "Chicken" else it will display "The user's favourite food does not include Chicken".

<logic:match name="UserForm" property="favouriteFood" value="Chicken">
The user's favourite food includes Chicken.
</logic:match>
<logic:notMatch name="UserForm" property="favouriteFood" value="Chicken">
The user's favourite food does not includes Chicken.
</logic:notMatch>

<logic:iterate />

The following code is used to iterate the ArrayList and display the contents.

<logic:iterate name="UserForm" property="hobbies" id="data">
<bean:write name="data" />
</logic:iterate>

On executing the example the following page will be displayed to the user.

You can download the source code of the Struts Logic Tag example by clicking on the Download link below.

Source: Download

Source + Lib: Download

Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: