ModelDriven Interface Example in Struts 2.0

ModelDriven Interface Example in Struts 2.0

Previous Home Next

 

ModelDriven Interface Example using Struts 2.0 the following tool are required for run this example
  • JDK 1.5
  • MyEclipse IDE
  • Server Tomcat 6.0
  • Struts 2.0 jar file
Directory Structure of Model Driven Interceptor Example in Struts 2.0 Using MyEclipse IDE



 
index.jsp

<%@taglib  uri="/struts-tags" prefix="s"%>

<s:form action="register">
<s:textfield name="user.name" label="Name"/>
<s:textfield name="user.mailId" label="MailID"/>
<s:textfield name="city" label="City"/>
<s:textfield name="state" label="State"/>
<s:submit value="Register"/>
</s:form>

web.xml

<?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>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</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="demo" extends="struts-default">
    <action name="register" class="mypack.RegisterAction">
    <result name="success">/hello.jsp</result>
    </action>
    </package>
    </struts>

Address.java

package mypack;

public class Address {
	String city,state;

	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
}

RegisterAction.java

package mypack;

import com.opensymphony.xwork2.ModelDriven;

public class RegisterAction implements ModelDriven<Address> {
	User user;
	public Address getModel(){
		user=new User();
		return user.getAddress();
	}
	public String execute()
	{
		return "success";
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

}

User.java

package mypack;

public class User {
	String name;
	String mailId;
	Address address;
	public User(){
		address=new Address();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMailId() {
		return mailId;
	}
	public void setMailId(String mailId) {
		this.mailId = mailId;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
}

hello.jsp

<%@ taglib  uri="/struts-tags" prefix="s"%>
<b>You are successfully registered with following detailes.
<br>Name:<s:property value="user.name"/>
<br>MailID:<s:property value="user.mailId"/>
<br>City:<s:property value="city"/>
<br>State:<s:property value="state"/>

Output





Previous Home Next