Implementation of Struts2.0 Model Driven Interceptor
Previous | Home | Next |
The ModelDriven interceptor is used to push a model object into the ValueStack show that its property can be directly access.ModelDriven interceptor work is association in with ModelDriven Interface which is provided by framework i.e if an action wants to push a model object into the valuestack with the help of ModelDriven interceptor then it need to implements ModelDriven interface. This interface provide one method getModel().public Object getModel();
Directory Structure of Model Driven Interceptor Example in Struts 2.0 Using MyEclipse IDEindex.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.javapackage 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.javapackage 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.javapackage 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 |