Struts 2 -> UI Example
Index.jsp
Register.jsp
Success.jsp
Struts.xml
Web.xml
Country.java
RegisterAction.java
Following figure show the struts
2 application structure.
web.xml
web.xml is used to configure the servlet
container properties of the First Simple appliation. The filter and the
filter-mapping elements are used to setup the Struts 2 FilterDispatcher. The
filter is mapped to the URL pattern "/*". This means all
the incoming request that targets to the Struts 2 action will be handled by FilterDispatcher
class.
<?xml version="1.0"
encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software
Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default"
extends="struts-default">
<action name="*Register"
method="{1}" class="com.RegisterAction">
<result
name="success">/success.jsp</result>
<result
name="populate">/register.jsp</result>
</action>
</package>
</struts>
Com.RegisterAction ->
RegisterAction is a class where you can write the action for the form.
In action tag * Register
means you can call RegisterAction class using (e.g insertRegister,updateRegister
etc) in place of * you can write any thing.
Index.jsp
<META
HTTP-EQUIV="Refresh"
CONTENT="0;URL=populateRegister.action">
Register.jsp
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags"
prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Registration Form</title>
</head>
<body>
<table border="1" align="center">
<s:form action="Register">
<tr>
<td><s:textfield name="userName"
label="User Name"></s:textfield></td>
</tr>
<tr>
<td><s:password name="password"
label="Password"></s:password></td>
</tr>
<tr>
<td><s:radio list="{'Male','Female'}"
name="gender" label="Gender"></s:radio></td>
</tr>
<tr>
<td><s:textarea name="address"
label="Address"></s:textarea></td>
</tr>
<tr>
<td><s:select list="countryList"
name="country" label="Country"
listKey="countryId"
listValue="countryName" headerKey="0"
headerValue="Country"></s:select></td>
</tr>
<tr>
<td><s:checkbox name="licence"
label="Licence Agreement"></s:checkbox></td>
</tr>
<tr>
<td><s:checkboxlist list="hobbyList"
name="hobby"
label="Hobbies"></s:checkboxlist></td>
</tr>
<tr>
<td><s:submit value="Register"
/></td>
</tr>
</s:form>
</table>
</body>
</html>
If you want to store static value in combo box then write
(list = “’a’,’b’,’c’”)
->listKey is store as a comb obox value and listValue is
display in combo box and header value is by default display in combo box.
Success.jsp
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags"
prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Successfully Register</title>
</head>
<body>
User Name : <s:property
value="userName"/></br>
Password : <s:property
value="password"/></br>
Gender : <s:property
value="gender"/></br>
Address : <s:property
value="address"/><br>
Country : <s:property value="country"/></br>
Licence : <s:property
value="licence"/></br>
Hobbies : <s:property value="hobby"/>
</body>
</html>
Country.java
package com;
public class Country {
public Country(int countryId, String countryName) {
this.countryId = countryId;
this.countryName = countryName;
}
private int countryId;
private String countryName;
public int getCountryId() {
return countryId;
}
public void setCountryId(int countryId) {
this.countryId = countryId;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
RegisterAction.java
package com;
import java.util.ArrayList;
import
com.opensymphony.xwork2.ActionSupport;
public class RegisterAction
extends ActionSupport{
private String userName;
private String password;
private String gender;
private String address;
private Boolean licence;
private String[] hobby;
private ArrayList<String> hobbyList;
private String country;
private ArrayList<Country> countryList;
public ArrayList<Country> getCountryList() {
return countryList;
}
public void setCountryList(ArrayList<Country> countryList)
{
this.countryList = countryList;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public ArrayList<String> getHobbyList() {
return hobbyList;
}
public void setHobbyList(ArrayList<String> hobbyList) {
this.hobbyList = hobbyList;
}
public Boolean getLicence() {
return licence;
}
public void setLicence(Boolean licence) {
this.licence = licence;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public RegisterAction(){
}
public String execute(){
return "success";
}
public String populate(){
hobbyList = new ArrayList<String>();
hobbyList.add("Cricket");
hobbyList.add("Music");
hobbyList.add("Reading");
countryList = new ArrayList<Country>();
countryList.add(new Country(1,"India"));
countryList.add(new Country(2,"USA"));
countryList.add(new Country(3,"Pakistan"));
return "populate";
}
public String getUserName() {
return userName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}