Send HTML Form data to struts2 action by Model Driven method

HTML form data can be transferred to Struts2 Action in three ways.

1. Normal Method with simple getters() and setters()
2. Object Backed Method
3. Model Driven Method

We will see the third method now.

Model Driven Method

Another way of transfering the form data directly inside the object is Model Driven approach. This approach is the best among all the three because for this approach no need to specify the field names deeply. We can define the fields as usual.

Create a simple jsp file index.jsp with struts2 form tags as below. Note that we never named the fields deeply like what we did earlier for object backed approach.

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

    <head>
        <title>Struts Data Transfer - Normal Transfer Method</title>
    </head>

    <body>
        <h3>Struts Data Transfer - Normal Transfer Method</h3>
        <s:form action="register">
            <s:textfield name="fname" label="First name" />
            <s:textfield name="lname" label="Last name" />
            <s:textfield name="email" label="Email" />
            <s:textfield name="age" label="Age" />
            <s:submit />
        </s:form>
    </body>

    </html>

So As per the jsp page when the form is getting submitted it will look for an action with name register. See out struts.xml below.

<?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" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />

    <package name="default" extends="struts-default" namespace="/">
        <action name="register" class="com.rajesh.dataTransfer.Register" method="register">
            <result name="success">success.jsp</result>
        </action>
    </package>
</struts>

In our Struts.xml we pointed the register action to register() method in the Register class. So when the form is getting submitted the control will go the register() method.  Now see the Register action class below.

package com.rajesh.dataTransfer;

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

@SuppressWarnings("rawtypes")
public class Register extends ActionSupport implements ModelDriven {
    private static final long serialVersionUID = -367986889632883043L;
    private User user = new User();

    public User getModel() {
        return user;
    }

    public String register() {
        System.out.println("Inside Action Method");
        System.out.println(user.getFname());
        System.out.println(user.getLname());
        System.out.println(user.getAge());
        System.out.println(user.getEmail());
        return SUCCESS;
    }
}

In our register action class we made two changes from Object backed method. One is the class implements ModelDriven interface and it’s method getModel(). So while the control look for setter methods it will run the getModel() method. Then it will take care of the data movement from form to object. Finally it will simply return the user object.

How this is achieved is by the framework interface ModelDriven. One important thing here is we have to create the User Object explicitly outside the method. And as usual the User class should have all the setters() and getters() for the required variables. See our User class below.

package com.rajesh.dataTransfer;

public class User {
    private String fname;
    private String lname;
    private int age;
    private String email;

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

So the Struts2 framework will execute the getModel() method before executing the actual action method, in our case register(). So while executing the getModel() method it will look for all the submitted data and if it matches with the method return type then  it will call the corresponding setter methods and return as object. Thus all the variables will be assigned to the user object and then returned.
[clear]
In our action method we just displayed the values in the console, then returning success message as result name. So as we mentioned the result page is success.jsp for string “SUCCESS” struts2 will load the page success.jsp to the user. See our success.jsp below.

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

    <head>
        <title>Struts Data Transfer - Normal Transfer Method</title>
    </head>

    <body>
        <h3>Struts Data Transfer - Normal Transfer Method</h3>
        <h4>User Data saved successfully</h4>
        First Name:<s:property value="fname" />
        <br>Last Name:<s:property value="lname" />
        <br>Email:<s:property value="email" />
        <br>Age:<s:property value="age" />
        <br>
    </body>

    </html>

What will happen for those tags like <s:property value =”fname”/> . Struts2 will look for getter() methods in the corresponding action class. As we discussed earlier due to ModelDriven interface implementation both the getter and setter process will be take care by the framework.

So it will run the getModel method and runs the getter methods inside the User object then it will return the object with all values. Then all the fields will be filled then returned to the user as a rendered page.

For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

[wpdm_package id=’554′]

If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.

Add a Comment

Your email address will not be published. Required fields are marked *