Struts 2 using model objects with ModelDriven example

By default, struts 2 will populate the fields declared in the action class with the request parameters (form data). In many situations, you may feel more comfortable when using model objects rather than declaring multiple fields in action class itself. If you need to use model objects in your struts 2 project and populate the fields of this object with the request parameters (form data), then you have to implement the action class with the interface ModelDriven.

In this example, let us see how to use model objects with or without implementing ModelDriven interface.

First let us create a model object.

Here Employee is the model

Employee.java


package net.javaonline.model;

public class Employee {

	String empCode ="";
	String name="";
	double taxableIncome=0;

	public double getTaxableIncome() {
		return taxableIncome;
	}
	public void setTaxableIncome(double taxableIncome) {
		this.taxableIncome = taxableIncome;
	}
	public String getEmpCode() {
		return empCode;
	}
	public void setEmpCode(String empcode) {
		this.empCode = empcode;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

} 

Action Class (TaxCalcAction)  with ModelDriven:

package net.javaonline.action;

import net.javaonline.model.Employee;
import net.javaonline.util.TaxCalc;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class TaxCalcAction extends ActionSupport implements ModelDriven{

	double tax=0;
	public double getTax() {
		return tax;
	}

	public void setTax(double tax) {
		this.tax = tax;
	}

	Employee empObj =new Employee();

	public Employee getEmpObj() {
		return empObj;
	}

	public void setEmpObj(Employee empObj) {
		this.empObj = empObj;
	}

	public String execute() throws Exception {
		TaxCalc tc = new TaxCalc();
		setTax(tc.getTax(empObj));
		return SUCCESS;
	}

	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		return empObj;
	}

}

The above action class is implemented with ModelDriven interface so that  the form data is automatically populated with the model object’s (Employee) fields when the user submits the form. When the action class is implemented with ModelDriven, you need to override getModel() method to return an object.

The Input JSP (EmpDetail.jsp)


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employee Details</title>
</head>
<body>
<s:actionerror/>
<s:form name="empForm" action="empAction.action" metod="post" >
<TABLE id="rtbl" width="350px" border="1">
  <TR>
  <td>
	Enter Employee Code : <s:textfield name="empCode"  label="Employee Code"/>
	Enter Name : <s:textfield name="name"  label="Name"/>
	Enter Taxable Income : <s:textfield name="taxableIncome"  label="taxable income"/>
<s:submit  value="Submit" type="button"/>
 </td>
</TR>
</TABLE>
</s:form>
</body>
</html>

The Result JSP (Tax.jsp)

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>Struts 2 ModelDriven example</h1>
<h2>Tax Details</h2>
	Employee Code : <s:property value="empCode" /><br>
	Name : <s:property value="name" /><br>
	Taxable Income : <s:property value="taxableIncome" /><br>
	Tax : <s:property value="tax" /><br>
</body>
</html> 

The same task can also be achieved without implementing ModelDriven interface.

Action Class (TaxCalcActionWithOutModelDriven):


package net.javaonline.action;

import net.javaonline.model.Employee;
import net.javaonline.util.TaxCalc;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class TaxCalcActionWithOutModelDriven extends ActionSupport{
	double tax=0;
	public double getTax() {
		return tax;
	}

	public void setTax(double tax) {
		this.tax = tax;
	}

	Employee empObj;;

	public Employee getEmpObj() {
		return empObj;
	}

	public void setEmpObj(Employee empObj) {
		this.empObj = empObj;
	}

	public String execute() throws Exception {
		TaxCalc tc = new TaxCalc();
		setTax(tc.getTax(empObj));
		return SUCCESS;

	}

} 

The above action class is not implemented with ModelDriven interface. To populate the form data with the model object’s (Employee) fields when the user submits the form, the parameters (Object’s member variables) should be bound to the object (embObj) as given in the below JSP

The Input JSP (EmpDetail1.jsp):

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employee Details</title>
</head>
<body>
<s:actionerror/>
<s:form name="empForm" action="empAction1.action" metod="post" >
<TABLE id="rtbl" width="350px" border="1">
  <TR>
  <td>
	Enter Employee Code : <s:textfield name="<strong>empObj.empCode</strong>"  label="Employee Code"/>
	Enter Name : <s:textfield name="<strong>empObj.name</strong>"  label="Name"/>
	Enter Taxable Income : <s:textfield name="<strong>empObj.taxableIncome</strong>"  label="taxable income"/>
<s:submit  value="Submit" type="button"/>
 </td>
</TR>
</TABLE>
</s:form>
</body>
</html>

The Result JSP (Tax.jsp)

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>Struts 2 ModelDriven example</h1>

<h2>Tax Details</h2>
Employee Code : <s:property value="<strong>empObj.empCode</strong>" /><br>
Name : <s:property value="<strong>empObj.name</strong>" /><br>
Taxable Income : <s:property value="<strong>empObj.taxableIncome</strong>" /><br>
Tax : <s:property value="tax" /><br>

</body>
</html> 

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.enable.DynamicMethodInvocation" 	value="false" />
	<constant name="struts.devMode" value="false" />

	<package name="default" extends="struts-default" namespace="/">
	    
	 <action name="empAction" class="net.javaonline.action.TaxCalcAction">
  		<result name="success">Tax.jsp</result>
  		<result name="input">Employee.jsp</result>
          </action>	   
    
 	 <action name="empAction1" class="net.javaonline.action.TaxCalcActionWithOutModelDriven">
  		<result name="success">Tax1.jsp</result>
  		<result name="input">Employee1.jsp</result>
        </action>	
    
       </package>		
		
</struts>

 

Running the above example

1. With ModelDriven Interface

http://localhost:8080/Structs2Dynamic/EmpDetail.jsp

model driven1

model driven res

2. Without ModelDriven Interface

without model driven

 

without model driven res

 

Reference : Apache Struts2 ModelDriven

You may also like

Leave a Reply

Be the First to Comment!