Tuesday, July 19, 2011

Struts - Step5. Adding ActionForm to HelloWorld

The following example shows the usage of ActionForm to accept values from the web page and process them.

AddUserForm.java

package org.mybusiness.strutstutorial.forms;

import org.apache.struts.action.ActionForm;

public class AddUserForm extends ActionForm {

    private static final long serialVersionUID = 1L;

    private String userId;

    private String password;

    private String name;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

AddUserAction.java

package org.mybusiness.strutstutorial.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.mybusiness.strutstutorial.forms.AddUserForm;

public class AddUserAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        AddUserForm userForm = (AddUserForm) form;

        System.out.println("User ID : " + userForm.getUserId());
        System.out.println("User Name : " + userForm.getName());

        return mapping.findForward("SUCCESS");
    }
}

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
    "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

    <form-beans>
        <form-bean name="addUserForm"
            type="org.mybusiness.strutstutorial.forms.AddUserForm" />
    </form-beans>

    <!--
        Using Global Forwards to forward to the desired view for Home Page
    -->
    <global-forwards>
        <forward name="HelloWorld" path="/WEB-INF/jsp/HelloWorld.jsp" />
    </global-forwards>

    <!-- Contains the URL Mapping -->
    <action-mappings>
        <action path="/helloworld"
            type="org.mybusiness.strutstutorial.actions.HelloWorldAction" />

        <action path="/addUser"
            type="org.mybusiness.strutstutorial.actions.AddUserAction" name="addUserForm"
            validate="false" input="/helloworld.do">
            <forward name="SUCCESS" path="/WEB-INF/jsp/AddUserSuccess.jsp"></forward>
        </action>
    </action-mappings>

    <message-resources parameter="application" />
</struts-config>

HelloWorld.jsp

The Struts html tags are used to get inputs from the user.

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
    prefix="html"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body bgcolor="#ffe">

<h2>Hello World. Welcome to my Struts based web application</h2>

<h4>The following message is displayed using struts bean:message
tag</h4>
<bean:message key="helloworldMsg" />
<br>
<br>
<html:form action="/addUser.do" method="POST">
    <table>
        <tr>
            <td align="right">User Id:</td>
            <td><html:text property="userId" size="15" /></td>
        </tr>
        <tr>
            <td align="right">User Name:</td>
            <td><html:text property="name" size="15" /></td>
        </tr>
        <tr>
            <td align="right">Password:</td>
            <td><html:password property="password" size="15" /></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Add User"></td>
        </tr>
    </table>
</html:form>

</body>
</html>

AddUserSuccess.jsp

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
    prefix="bean"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add User Success</title>
</head>
<body>

<h2>User Added Successfully!!</h2>

User Id:
<bean:write name="addUserForm" property="userId" />
<br>
User Name:
<bean:write name="addUserForm" property="name" />
<br>

</body>
</html>

Screenshots



No comments:

Post a Comment