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.


1.<%@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.
1.<jsp-config>
2.<taglib>
3.<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
4.<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
5.</taglib>
6.</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.
1.private String name;
2.private int age;
3.private float height;
4.private float weight;
5.private String
favouriteFood;
6.private ArrayList hobbies;
Initially we set
default values for these attributes in the UserAction.
01.public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throwsException {
02.ArrayList hobbiesList
= new ArrayList();
03.UserForm userForm =
(UserForm)form;
04.userForm.setName("Eswar");
05.userForm.setAge(21);
06.userForm.setHeight(5.11f);
07.userForm.setWeight(70f);
08.userForm.setFavouriteFood("Fish
and Chicken");
09.hobbiesList.add("Music");
10.hobbiesList.add("Art");
11.hobbiesList.add("Dance");
12.userForm.setHobbies(hobbiesList);
13.return mapping.findForward(SUCCESS);
14.}
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.
1.<logic:present name="UserForm" property="name">
2.The User Name is Eswar.
3.</logic:present>
4.<logic:notPresent name="UserForm" property="name">
5.The User is not Present.
6.</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."
1.<logic:equal name="UserForm" property="age" value="18">
2.The age is equal to 18.
3.</logic:equal>
4.<logic:notEqual name="UserForm" property="age" value="18">
5.The age is not equal to 18.
6.</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.
1.<logic:greaterEqual name="UserForm" property="height" value="5.11">
2.The height is greater than or equal to
5.11
3.</logic:greaterEqual>
4.<logic:greaterThan name="UserForm" property="height" value="5.11">
5.The height is greater than 5.11
6.</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.
1.<logic:lessEqual name="UserForm" property="weight" value="70">
2.The weight is less than or equal to 70.
3.</logic:lessEqual>
4.<logic:lessThan name="UserForm" property="weight" value="70">
5.The weight is less than 70.
6.</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".
1.<logic:match name="UserForm" property="favouriteFood" value="Chicken">
2.The user's favourite food includes
Chicken.
3.</logic:match>
4.<logic:notMatch name="UserForm" property="favouriteFood" value="Chicken">
5.The user's favourite food does not
includes Chicken.
6.</logic:notMatch>
<logic:iterate
/>
The following code
is used to iterate the ArrayList and display the contents.
1.<logic:iterate name="UserForm" property="hobbies" id="data">
2.<bean:write name="data" />
3.</logic:iterate>
No comments:
Post a Comment