Struts 2 Form Data with ModelDriven Example

 Download this Project

In the Struts 2 Form Submit and Capturing Form Data Example we learned to capture the form data using struts2. In this article, we will implement the ModelDriven interface in Action class to capture the form data into an Object automatically.

1. Development Environment 

  1. Eclipse
  2. JDK 1.8
  3. Tomcat 8.5
  4. Maven

2. Create a Maven Web Project

Below is my dynamic web project struts2-mode-driven structure in eclipse. In this example, we are using struts 2.1.8 version.

This project has the following actions,

  • user-form.action: It has a simple form to capture some basic details entered by the user.
  • user-form-data.action: It will show you the submitted form data.

Below is the source code of the struts2-model-driven project.

Code:

package com.simba.demo;

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

/**
 * @author Simba
 *
 */
public class UserFormDataAction extends ActionSupport implements ModelDriven<User> {

  private User user=new User();

  public UserFormDataAction() {
  }

  public String userForm() {
    return SUCCESS;
  }
  public String userFormData() {
    return SUCCESS;
  }

  @Override
  public User getModel() {
    return this.user;
  }
}
package com.simba.demo;

/**
 * @author Simba
 *
 */
public class User {

  private String name;
  private String gender;
  private String description;
  private Integer age;

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getGender() {
    return gender;
  }
  public void setGender(String gender) {
    this.gender = gender;
  }
  public String getDescription() {
    return description;
  }
  public void setDescription(String description) {
    this.description = description;
  }
  public Integer getAge() {
    return age;
  }
  public void setAge(Integer age) {
    this.age = age;
  }

}

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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=ISO-8859-1">
<title>User Form</title>
</head>
<body>
  <h1>Enter User Details - ModelDriven</h1>
  <s:form action="user-form-data">
    <table>
      <tr>
        <td>Name :</td>
        <td><s:textfield name="name" theme="simple"></s:textfield></td>
      </tr>
      <tr>
        <td>Gender :</td>
        <td><s:radio name="gender" list="{'Male','Female'}"  theme="simple"></s:radio></td>
      </tr>
      <tr>
        <td>Description :</td>
        <td><s:textarea name="description" theme="simple"></s:textarea></td>
      </tr>
      <tr>
        <td>Age :</td>
        <td><s:select name="age" list="{18,19,20,21,22}" theme="simple"></s:select></td>
      </tr>
      <tr>
        <td><s:submit></s:submit></td>
      </tr>
    </table>
  </s:form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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=ISO-8859-1">
<title>User Form Data - ModelDriven</title>
</head>
<body>
  <h1>Submitted User Details</h1>
  <s:form>
    <table>
      <tr>
        <td>Name :</td>
        <td><s:property value="name" /></td>
      </tr>
      <tr>
        <td>Gender :</td>
        <td><s:property value="gender" /></td>
      </tr>
      <tr>
        <td>Description :</td>
        <td><s:property value="description" /></td>
      </tr>
      <tr>
        <td>Age :</td>
        <td><s:property value="age" /></td>
      </tr>
    </table>
  </s:form>
</body>
</html>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"classpath://struts-2.0.dtd">

<struts>

  <package name="userFormData" namespace="/" extends="struts-default">
    <action name="user-form" class="com.simba.demo.UserFormDataAction" method="userForm">
      <result>/WEB-INF/jsp/user-form.jsp</result>
    </action>
    <action name="user-form-data" class="com.simba.demo.UserFormDataAction" method="userFormData">
      <result>/WEB-INF/jsp/user-form-data.jsp</result>
    </action>
  </package>

</struts>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>struts2-model-driven</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>struts2-model-driven</groupId>
  <artifactId>struts2-model-driven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.1.8</version>
    </dependency>
  </dependencies>
</project>

3. Testing

In your eclipse configure tomcat and run your application by selecting Run on server.