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


commons-collections-3.1.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.8.jar
xwork-core-2.3.8.jar

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
index.jsp


<% @taglib uri= "/struts-tags" prefix= "s" %>
<html>
<head>
<title>Model Driven Action Demo</title>
</head>
<body>
<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" />
</s:form>
</body>
</html>

EmployeeSuccess.jsp

<% @taglib uri= "/struts-tags" prefix= "s" %>
<html>
<head>
<title>Employee Suucess Page</title>
</head>
<body>
<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>
</h2>
</body>
</html>

Step 5: Create Model class Employee.java -


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 EmployeeAction1.java -


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 () ;
@Override
public Employee getModel ()
{
return emp;
}
@Override
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"
"http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
< 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>
</action>
</ package >
</struts>

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

 


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


Write your Comment

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