Tuesday, August 6, 2013

Struts 2 UI Tags Tutorial

Struts 2 UI Tags are simple and easy to use. You need not write any HTML code, the UI tags will automatically generate them for you based on the theme you select. By default the XHTML theme is used. The XHTML theme uses tables to position the form elements.
In this example you wil see how to create a registration page using Struts 2 UI tags. You will also learn how to pre populate the form fields, set default values to it and to retrive the values back in the jsp page.
The register.jsp looks like this
The following code is used to create the register.jsp page
01.<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02.pageEncoding="ISO-8859-1"%>
03.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.<%@taglib uri="/struts-tags" prefix="s"%>
05.<html>
06.<head>
07.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
08.<title>Register Page</title>
09.</head>
10.<body>
11.<s:form action="Register">
12.<s:textfield name="userName" label="User Name" />
13.<s:password name="password" label="Password" />
14.<s:radio name="gender" label="Gender" list="{'Male','Female'}" />
15.<s:select name="country" list="countryList" listKey="countryId"listValue="countryName" headerKey="0" headerValue="Country"label="Select a country" />
16.<s:textarea name="about" label="About You" />
17.<s:checkboxlist list="communityList" name="community" label="Community" />
18.<s:checkbox name="mailingList"
19.label="Would you like to join our mailing list?" />
20.<s:submit />
21.</s:form>
22.</body>
23.</html>
If you view the source of this page you can see the HTML codes generated based on the XHTML theme.

Struts 2 ValueStack

Now lets understand how the UI tags work. In Struts 2 ValueStack is the place where the data associated with processing of the request is stored. So all the form properties will be stored on theValueStack. The name attribute of the UI tag is the one which links the property on the ValueStack.
The next important attribute of the UI tag that you need to understand is the value attribute. If you like to populate some default value for a specific field then you need to set that value attribute to that value.
The following code will by default set the value of the textfield to "Eswar"
1.<s:textfield name="userName" label="User Name" value="Eswar"/>
Here we directly specify the value in the jsp page, instead if you want to set it throught Action then, you can have a property like defaultName and set its value to the desired name. In this case the code will look like this.
1.<s:textfield name="userName" label="User Name" value="defaultName"/>
The property defaultName is stored on the ValueStack so its value will be set to the textfield. If you think you don't need a seperate form property to do this, then you can set the userName property itself to the desired value. In this case you need not specify the value attribute seperately. In this example we populate the community in this way.
The value set in the label attribute of the UI tags will be used to render the label for that particular field while generating the HTML code.
Now lets see the flow of the example. First the index.jsp page will be invoked by the framework.
1.index.jsp
2.~~~~~~~~~
3.<META HTTP-EQUIV="Refresh" CONTENT="0;URL=populateRegister.action">
Here we forward the request to the populateRegister URL. Based on the mapping done in thestruts.xml file the populate() method in the RegisterAction class will be called. Here the mapping is done using the dynamic method invocation feature of Struts 2. The struts.xml file contains the following mapping.
01.<!DOCTYPE struts PUBLIC
02."-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. 
05.<struts>
06.<package name="default" extends="struts-default">
07.<action name="*Register" method="{1}"class="vaannila.RegisterAction">
08.<result name="populate">/register.jsp</result>
09.<result name="input">/register.jsp</result>
10.<result name="success">/success.jsp</result>
11.</action>
12.</package>
13.</struts>
The register action class contains the form properties and the corresponding getter and setter methods. It also contains the execute() and populate() methods. In the populate method we first populate the values and then set the default values for the form fields. The RegisterAction class contains the following code.
001.package vaannila;
002. 
003.import java.util.ArrayList;
004. 
005.import com.opensymphony.xwork2.ActionSupport;
006. 
007.public class RegisterAction extends ActionSupport {
008. 
009.private String userName;
010. 
011.private String password;
012. 
013.private String gender;
014. 
015.private String about;
016. 
017.private String country;
018. 
019.private ArrayList<Country> countryList;
020. 
021.private String[] community;
022. 
023.private ArrayList<String> communityList;
024. 
025.private Boolean  mailingList;
026. 
027.public String populate() {
028. 
029.countryList = new ArrayList<Country>();
030.countryList.add(new Country(1"India"));
031.countryList.add(new Country(2"USA"));
032.countryList.add(new Country(3"France"));
033. 
034.communityList = new ArrayList<String>();
035.communityList.add("Java");
036.communityList.add(".Net");
037.communityList.add("SOA");
038. 
039.community = new String[]{"Java",".Net"};
040.mailingList = true;
041. 
042.return "populate";
043.}
044. 
045.public String execute() {
046.return SUCCESS;
047.}
048. 
049.public String getUserName() {
050.return userName;
051.}
052. 
053.public void setUserName(String userName) {
054.this.userName = userName;
055.}
056. 
057.public String getPassword() {
058.return password;
059.}
060. 
061.public void setPassword(String password) {
062.this.password = password;
063.}
064. 
065.public String getGender() {
066.return gender;
067.}
068. 
069.public void setGender(String gender) {
070.this.gender = gender;
071.}
072. 
073.public String getAbout() {
074.return about;
075.}
076. 
077.public void setAbout(String about) {
078.this.about = about;
079.}
080. 
081.public String getCountry() {
082.return country;
083.}
084. 
085.public void setCountry(String country) {
086.this.country = country;
087.}
088. 
089.public ArrayList<Country> getCountryList() {
090.return countryList;
091.}
092. 
093.public void setCountryList(ArrayList<Country> countryList) {
094.this.countryList = countryList;
095.}
096. 
097.public String[] getCommunity() {
098.return community;
099.}
100. 
101.public void setCommunity(String[] community) {
102.this.community = community;
103.}
104. 
105.public ArrayList<String> getCommunityList() {
106.return communityList;
107.}
108. 
109.public void setCommunityList(ArrayList<String> communityList) {
110.this.communityList = communityList;
111.}
112. 
113.public Boolean getMailingList() {
114.return mailingList;
115.}
116. 
117.public void setMailingList(Boolean mailingList) {
118.this.mailingList = mailingList;
119.}
120. 
121.}

Textfield and Password Tags

Now lets see each UI tag in detail. The textfiled tag is used to create a textfield and password tag is used to create a password field. These tags are simple and uses only the common attributes discussed before.
1.<s:textfield name="userName" label="User Name" />
2.<s:password name="password" label="Password" />

Radio Tag

To create radio buttons we use radio tag. The list attribute of the radio tag is used to specify the option values. The value of the list attribute can be a Collection, Map, Array or Iterator. Here we use Array.
1.<s:radio name="gender" label="Gender" list="{'Male','Female'}" />

Select Tag

We dispaly the country dropdown using the select tag. Here we specify the option values using thecountryList property of the RegisterAction class. The countryList is of type ArrayList and contain values of type Country. The Country class has countryId and countryName attribute. ThecountryName holds the country value to be display in the frontend and the countryId holds the id value to store it in the backend. Here countryId is the key and the countryName is the value. We specify this in the select tag using the listKey and listValue attribute. The first value can be specified using theheaderValue attribute and the corresponding key value is specified using the headerKey attribute.
1.<s:select name="country" list="countryList" listKey="countryId"listValue="countryName" headerKey="0" headerValue="Country"
2.label="Select a country" />

Textarea Tag

The textarea tag is used to create a textarea.
1.<s:textarea name="about" label="About You" />

Checkboxlist Tag

The checkboxlist tag is similar to that of the select tag, the only difference is that it displays boxes for each option instead of a dropdown. It returns an array of String values.
1.<s:checkboxlist list="communityList" name="community" label="Community"/>

Checkbox Tag

The checkbox tag returns a boolean value. If the checkbox is checked then true is returned else false is returned.
1.<s:checkbox name="mailingList" label="Would you like to join our mailing list?" />

Submit Tag

The submit tag is used to create the Submit button
1.<s:submit />
Now lets enter the details and submit the form. The execute() method in the RegisterAction class will be invoked this time and the user will be forwarded to the success.jsp page.
01.success.jsp
02.-----------
03.<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
04.pageEncoding="ISO-8859-1"%>
05.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
06.<%@taglib uri="/struts-tags"  prefix="s"%>
07.<html>
08.<head>
09.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
10.<title>Details Page</title>
11.</head>
12.<body>
13.User Name: <s:property value="userName" />
14. 
15.Gender: <s:property value="gender" />
16. 
17.Country: <s:property value="country" />
18. 
19.About You: <s:property value="about" />
20. 
21.Community: <s:property value="community" />
22. 
23.Mailing List: <s:property value="mailingList" />
24.</body>
25.</html>
Now lets enter the following details and submit the form.
The following registration details will be displayed to the user.

No comments:

Post a Comment