Send HTML Form data to struts2 action by Normal 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 first method now.

Normal Method with simple getters() and setters()

Create a simple jsp file index.jsp with form (struts2 tags) as 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>
        <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. As per struts2 design the control will look methods for all available values. In out case for fname,lname,email and age. It will search for methods setFname(),setLname(),setEmail() and setAge() respectively. Now see our Register class in below.

package com.rajesh.dataTransfer;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {

    private static final long serialVersionUID = -367986889632883043L;
    private String fname;
    private String lname;
    private int age;
    private String email;
    private User user;;

    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;
    }

    public String register() {
        System.out.println("Inside Action Method");
        user = new User();
        user.setAge(getAge());
        user.setEmail(getEmail());
        user.setLname(getLname());
        user.setFname(getFname());
        System.out.println(user.getFname());
        System.out.println(user.getLname());
        System.out.println(user.getAge());
        System.out.println(user.getEmail());
        return SUCCESS;
    }
}

As we have all the required methods, the Struts2 framework will execute all those methods before executing the actual action method, in our case register(). So after executing all the methods the control will go to register method. So all the variables are available now for operation.
[clear]
In our action method we just displayed the values in the console, then returning success message as result name.
stdata3So 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 get methods in the corresponding action class. As we have all get methods getEmail(), getLname(), getFname() and getAge() in the action class those will get executed and the value will be placed there.

stdata1 stdata2 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=’558′]

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 *