Struts 2 modelDriven interceptor example


The modelDriven interceptor makes other model item as the default object of valuestack.

Bydefault, activity object is the default object of valuestack.

To utilize the modelDriven interceptor, you have to execute ModelDriven interface in your activity class and abrogate its technique getModel().

It is found in the default stack bydefault. So you don't have to determine it explicitely.

Parameters of modelDriven interceptor

There is no parameter characterized for modelDriven interceptor.

Example of modelDriven interceptor

  1. <action name="login" class="com.javacodegeeks.LoginAction">  
  2.     <interceptor-ref name="params"/>  
  3.     <interceptor-ref name="modelDriven"/>  
  4.     <result name="success">login-success.jsp</result>  
  5. </action>  

Full case of modelDriven interceptor

Let's see the full case of modelDriven interceptor.

File: index.jsp
  1. <%@ taglib uri="/struts tags" prefix="s" %>  
  2.   
  3. <s:form action="login">  
  4. <s:textfield name="name" label="Name"></s:textfield>  
  5. <s:password name="password" label="Password"></s:password>  
  6. <s:submit value="login"></s:submit>  
  7. </s:form>  
File: struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-/Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts 2.1.dtd">  
  3. <struts>  
  4.   
  5. <package name="abc" extends="struts-default" >  
  6.   
  7. <action name="login" class="com.javacodegeeks.Login">  
  8. <result name="success" >/login-success.jsp</result>  
  9. <result name="error">/login-error.jsp</result>  
  10. </action>  
  11.   
  12. </package>  
  13. </struts>      
  1. package com.javacodegeeks;  
  2.   
  3. public class User {  
  4. private String name,password;  
  5. //getters and setters  
  6. }  
File: login-success.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. Welcome, <s:property value="name"/>  
File: login-error.jsp
  1. Sorry, username or password error!  
  2. <jsp:include page="index.jsp"></jsp:include>  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition