Struts 2 Model Driven Approach
In this demo you will see how to implement model driven approach in struts 2 application. Basically when use struts application there is two way to populate request:
- Bound request parameters directly into the fields in your action class.
- Implements interface com.opensymphony.xwork2.ModelDriven in your action class and override its method getModel() method and return object from it, then struts framework populate fields of this object with request parameters
Steps:
- Create maven project name: Struts2ModelDriven
- Final project structure:
- pom.xml dependency:
<dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.16.3</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.16.3</version> </dependency> </dependencies>
- index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Struts 2 Model Driven Approach</title> <s:head /> </head> <body> <h3 style="color: green"> <s:text name="Struts 2 Model Driven Approach" /> </h3> <s:form action="modelDrivenAction" namespace="/" method="post" name="strutsForm" enctype="multipart/form-data"> <s:textfield label="Name" name="name" /> <s:textfield label="Contact Number" name="contactNumber" /> <s:textfield label="location" name="location" /> <s:checkbox label="Male" name="male" /> <s:textarea cols="20" rows="3" label="Description" name="description" /> <s:submit /> </s:form> </body> </html>
- success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Struts 2 Model Driven Approach</title> </head> <body> <h3 style="color: green"> <s:text name="Below data entered" /> </h3> <p>Name: <s:property value="name" /></p> <p>Contact Number: <s:property value="contactNumber" /></p> <p>Location: <s:property value="location" /></p> <p>Gender (Male): <s:property value="male" /></p> <p>Description: <s:property value="description" /></p> </body> </html>
- Person.java
package com.javahonk.action; public class Person { private String name; private Integer contactNumber; private String location; private Boolean male; private String description; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getContactNumber() { return contactNumber; } public void setContactNumber(Integer contactNumber) { this.contactNumber = contactNumber; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public Boolean getMale() { return male; } public void setMale(Boolean male) { this.male = male; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
- ModelDrivenAction.java
package com.javahonk.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @Results({ @Result(name = "error", location = "/index.jsp"), @Result(name = "success", location = "/success.jsp"), @Result(name = "input", location = "/index.jsp") }) public class ModelDrivenAction extends ActionSupport implements ModelDriven<Person>{ private static final long serialVersionUID = 1L; @Override @Action(value = "/modelDrivenAction") public String execute() throws Exception { return ActionSupport.SUCCESS; } /* (non-Javadoc) * @see com.opensymphony.xwork2.ModelDriven#getModel() */ public Person getModel() { return new Person(); } }
- For demo we will run on tomcat server. Please use this tutorial: Configure and Run Tomcat server in eclipse. Now right click project –>Run As –> Run on server you will see below page:
- Enter data in input fields:
- Click Submit button you will see output of entered data as below:
For more information on Model driven approach please read from struts 2 site here
Download Project: Struts2ModelDriven