jars required
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.18.jar
ognl-3.0.4.jar
struts2-core-2.3.1.2.jar
xwork-core-2.3.1.2.jar
commons-lang-2.5.jar
javassist-3.11.0.GA.jar
struts2-dojo-plugin-2.3.1.2.jar
put the above jars in lib folder
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.18.jar
ognl-3.0.4.jar
struts2-core-2.3.1.2.jar
xwork-core-2.3.1.2.jar
commons-lang-2.5.jar
javassist-3.11.0.GA.jar
struts2-dojo-plugin-2.3.1.2.jar
put the above jars in lib folder
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2.3_ProgramaticValidations</display-name>
<welcome-file-list>
<welcome-file>user.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.properties
struts.custom.i18n.resources=messages
messages.properties
title=Struts2 ModelDriven Application
name=Username
age=Age
address=Address
sex=Sex
hobby=Hobbies
dob=Date Of Birth
country=Country
occupation=Occupation
label.button=Submit
user.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<sx:head />
</head>
<body>
<h2>
<s:text name="title"></s:text>
</h2>
<s:form action="userAction">
<s:textfield name="name" key="name"></s:textfield>
<s:textfield name="age" key="age"></s:textfield>
<s:textarea name="address" key="address"></s:textarea>
<s:radio list="{'M','F'}" name="sex" key="sex"></s:radio>
<s:doubleselect
doubleList="top == 'Technical' ?
{'I.T', 'Hardware'} : {'Accounting', 'H.R'}"
list="{'Technical','Other'}" doubleName="occupations2"
name="occupation" key="occupation"></s:doubleselect>
<s:checkboxlist list="{'Music','Art','Dance'}" name="hobby"
key="hobby"></s:checkboxlist>
<sx:datetimepicker name="dob" key="dob"></sx:datetimepicker>
<sx:autocompleter list="{'India','usa','uk'}" name="country"
key="country"></sx:autocompleter>
<s:submit key="label.button"></s:submit>
</s:form>
</body>
</html>
user.java
package com.usr.action;
import java.util.Date;
public class User {
private String name;
private int age;
private String sex;
private Date dob;
private String[] hobby;
private String address;
private String occupation;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
}
UserAction.java
package com.usr.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements ModelDriven {
private User user=new User();
@Override
public Object getModel() {
// TODO Auto-generated method stub
return user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute(){
return "success";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default"></include>
<package name="default" extends="struts-default">
<action name="userAction" class="com.usr.action.UserAction">
<result name="success">/WEB-INF/pages/UserSuccess.jsp</result>
</action>
</package>
</struts>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default"></include>
<package name="default" extends="struts-default">
<action name="userAction" class="com.usr.action.UserAction">
<result name="success">/WEB-INF/pages/UserSuccess.jsp</result>
</action>
</package>
</struts>
UserSuccess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<hr>
success page.......
<hr>
Name:
<s:property value="name" />
<br> Age:
<s:property value="age" />
<br> Address:
<s:property value="address" />
<br> DateOfBirth:
<s:property value="dob" />
<br> Hobby:
<s:property value="hobby" />
<br> Occupation:
<s:property value="occupation" />
<br> Sex:
<s:property value="sex" />
<br> Country:
<s:property value="country" />
</body>
</html>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.