Tuesday 12 May 2015

Struts2 ModelDriven Interface

In Struts2, request parameters are directly bound to the fields in action class and this class is placed in top of the stack when the action is executed.  By implementing ModelDriven interface, you can map the request parameters to some other model object, other than action class.

Any Action class implementing the ModelDriven interface must supply a getModel() method which returns the object that represents the action's model. All the request parameters are mapped to this model object returned by getModel(). Following example explains in detail.

Step 1: Create new maven project “struts_tutorial”. Follow first 3 steps from the following link to make the project setup.


Step 2: Create new jsp file “register.jsp”.

register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form method="post" action="registration">
  User Name : <input type="text" name="userName" /><br /> passowrd : <input
   type="password" name="password" /><br /> mailId : <input
   type="text" name="mailId" /><br /> <input type="submit"
   value="submit" />
 </form>
</body>
</html>


Create helloWorld.jsp file with following code.

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <h1>Hello World</h1>
</body>
</html>

Step 3: create struts.xml. Since Struts 2 requires struts.xml to be present in classes folder. So create struts.xml file under the WebContent/WEB-INF/classes folder. Eclipse does not create the "classes" folder by default, so you need to do this yourself. To do this, right click on the WEB-INF folder in the project explorer and select New > Folder.

struts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <package name="default" extends="struts-default">
  <action name="registration" class="learn.struts.RegisterAction">
   <result name="hello">/helloWorld.jsp</result>
  </action>
 </package>
</struts>

To use ModelDriven actions, make sure that the Model Driven Interceptor is applied to your action. This interceptor is part of the default interceptor stack defaultStack so it is applied to all actions by default.
Step 4: Create a package “learn.struts”. Create class RegisterAction.java inside this package.
package learn.struts;

import com.opensymphony.xwork2.ModelDriven;

public class RegisterAction implements ModelDriven<Employee> {
 
 /* Must initialize this instance */
 private Employee employee = new Employee();

 public String execute() {
  System.out.println("Employee Details");
  System.out.println(employee.getUserName());
  System.out.println(employee.getMailId());
  return "hello";
 }

 @Override
 public Employee getModel() {
  return employee;
 }

}


Create model class Employee.java
package learn.struts;

public class Employee {
 private String userName;
 private String password;
 private String mailId;

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getMailId() {
  return mailId;
 }

 public void setMailId(String mailId) {
  this.mailId = mailId;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [userName=").append(userName)
    .append(", password=").append(password).append(", mailId=")
    .append(mailId).append("]");
  return builder.toString();
 }

}
Run register.jsp and check the output in console.

Final project structure looks like following.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment