Model Driven action in Struts2 framework

In Struts 2, action class itself treats as model class. And Struts 2 request parameters are bound directly to fields in the actions class. Sometime the request fields keep in separate class so it can be be used in other scenario also, and this domain class associate with Action class.

In order to implement the above concept, you create action class by implementing com.opensymphony.xwork2.ModelDriven interface. This interface contains getModel() method which return use defined domain object. Struts will then populate the fields of this object with the request parameters, and this object will be placed on top of the stack once the action is executed.

To implement ModelDriven action concept in your application, use ModelDriven interceptor, which is is part of the default interceptor stack defaultStack so it is applied to all actions by default.

public class EmployeeAction1 extends ActionSupport implements ModelDriven<Employee>

Let's demonstrate Model Driven action in Struts 2

Step 1: Create Dynamic web project: "Struts2ModelDriven"
Struts2 model driven action

Step 2: Copy following jar files in WEB-INF\lib folder


Step 3: Edit deployment descriptor web.xml. [ Click here If you are not familiar with Struts2 application environment setup in Eclipse. ]

Step 4: Create input page WebContent\index.jsp and View component WebContent\WEB-INF\pages\EmployeeSuucess.jsp

<% @taglib uri= "/struts-tags" prefix= "s" %>
<title>Model Driven Action Demo</title>
<h2>Employee Registration Form</h2>
<s:form action= "EmployeeAction1" method= "post" >
<s:textfield label= "Enter Code" name= "code" />
<s:textfield label= "Enter Name" name= "name" />
<s:textfield label= "Enter City" name= "city" />
<s:textfield label= "Enter Salary" name= "salary" value= "0" />
<s:submit label= "Submit" />
<s:reset label= "Reset" />


<% @taglib uri= "/struts-tags" prefix= "s" %>
<title>Employee Suucess Page</title>
<h2>Employee details are<br>
Employee Code :<s:property value= "code" /><br>
Employee Name :<s:property value= "name" /><br>
Employee City :<s:property value= "city" /><br>
Employee Salary :<s:property value= "salary" /><br>
<a href= "index.jsp" > Try Agaian</a>

Step 5: Create Model class -

package techmyguru.actions;
public class Employee {
private String code;
private String name;
private String city;
private Double salary;
// all getter/setter methods

Step 6: Create Action class -

package techmyguru.actions;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class EmployeeAction1 extends ActionSupport implements ModelDriven<Employee>
Employee emp = new Employee () ;
public Employee getModel ()
return emp;
public String execute () throws Exception {
return SUCCESS;

Step 7: Create src\struts.xml and add map configuration between View, Action and Results

<?xml version= "1.0" encoding= "UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"" >
< package name= "default" extends = "struts-default" >
<action name= "EmployeeAction1" class = "techmyguru.actions.EmployeeAction1" >
<result name= "success" >/WEB-INF/pages/EmployeeSuucess.jsp</result>
<result name= "input" >/index.jsp</result>
</ package >

Step 8: Verify the directory structure as below -
Struts2 model driven action

Step 9: Run the application and get output :
Right click on the project -> Run As -> Run on Server and see screen -
Struts2 Model driven Output1

Fill the form ->click submit button and see th output

Struts2 Model driven Output2

Previous Download Source Code Next


Share your valuable comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code :

Comment by : raj gopal527 April 18th, 2014
Comment : cool tutorial