Friday, 10 July 2015

Struts 2 modelDriven interceptor example

The modelDriven interceptor makes other model object as the default object of valuestack.
Bydefault, action object is the default object of valuestack.
To use the modelDriven interceptor, you need to implement ModelDriven interface in your action class and override its method getModel().
It is found in the default stack bydefault. So you don't need to specify it explicitely.

Parameters of modelDriven interceptor

There is no parameter defined for modelDriven interceptor.

Example of modelDriven interceptor

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

Full example of modelDriven interceptor

Let's see the full example 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.javatpoint.Login">  
  8. <result name="success" >/login-success.jsp</result>  
  9. <result name="error">/login-error.jsp</result>  
  10. </action>  
  11.   
  12. </package>  
  13. </struts>      
File: Login.java
  1. package com.javatpoint;  
  2. import com.opensymphony.xwork2.ModelDriven;  
  3.   
  4. public class Login implements ModelDriven<User>{  
  5. private User user;  
  6.   
  7. public User getUser() {  
  8.     return user;  
  9. }  
  10.   
  11. public void setUser(User user) {  
  12.     this.user = user;  
  13. }  
  14. public User getModel(){  
  15.     user=new User();  
  16.     return user;  
  17. }  
  18. public String execute(){  
  19. if(user.getPassword().equals("admin")){  
  20.     return "success";  
  21. }  
  22. else{  
  23.     return "error";  
  24. }  
  25. }  
  26.   
  27. }  
File: User.java
  1. package com.javatpoint;  
  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>  

Output

struts 2 modeldriven interceptor example output 1 struts 2 modeldriven interceptor example output 2

If you don't implement the ModelDriven interface, you need to use user.name and user.password field names in index.jsp file otherwise given value will not be set.