Struts Gossip: Use ActionForm

Source: Internet
Author: User

ActionForm is an object-based representation of a table. It is designed as a JavaBean. You must inherit org. apache. struts. action. actionForm is used to design a single table object and provides the getter and setter methods of the rule. When necessary, you can use the reset () and validate () methods inherited by the statement () for Struts group call.
When the customer request is submitted to RequestProcessor, RequestProcessor will know from ActionMapping the ActionForm object used, which is set in the struts-config.xml, if the required ActionForm does not exist, generate one, and use it all the time later, the ActionMapping and ActionForm objects will be converted to the Action by the actual operation number.
In Struts 1.1, after an ActionForm is generated, the processPopulate () method of the RequestProcessor line is called. First, the ActionForm reset () method is called, in this example, you can perform operations that refresh the vigilance of ActionForm. Then, the values of table orders are compared with the setter of ActionForm, if the famous values match, set the values of the table orders to the corresponding values.
The following example shows how to use ActionForm:

  • UserForm. java
package onlyfun.caterpillar; 

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class UserForm extends ActionForm {
private String username;
private String password;

public void setUsername(String username) {
this.username = username;
}

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

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public void reset(ActionMapping mapping,
HttpServletRequest request) {
username = null;
password = null;
}
}

You must define in the struts-config.xml which ActionForm object the Action object uses:

  • Struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!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="userForm"
            type="onlyfun.caterpillar.UserForm"/>
    </form-beans>

    <action-mappings>
        <action
            path="/login"
            type="onlyfun.caterpillar.LoginAction"
            name="userForm">
            <forward
                name="helloUser"
                path="/WEB-INF/pages/hello.jsp"/>
            <forward
                name="loginFail"
                path="/WEB-INF/pages/fail.jsp"/>
        </action>
    </action-mappings>
</struts-config> 

In this example, the <form-bean> criterion defines the ActionForm object used and its userForm name. In the <action> criterion, loginAction specifies the ActionForm used by userForm. The LoginAction types are as follows:

  • LoginAction. java
package onlyfun.caterpillar;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class LoginAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String username = ((UserForm) form).getUsername();
String password = ((UserForm) form).getPassword();

request.setAttribute("username", username);

if(username.equals("caterpillar") &&
password.equals("1234")) {
return mapping.findForward("helloUser");
}

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

ActionForm is used as the upload data to the execute () method. After the user's name and password are invalid, search for the helloUser's ActionForward object and send the response, this causes RequestProcessor to send requests to hello. jsp:

  • Hello. jsp
<title>Hello, ${username} !</title> 
<body>
<H1>Hello, ${username} !</H1>
<H1>This is your secret gift!!</H1>
</body>

If the Verification Code fails, search for the ActionForward object of loginFail and restore it, that is, fail. jsp:

  • Fail. jsp
<title>Sorry!</title> 
<body>
<H1>Sorry, ${username} !</H1>
<H1>You must provide correct login information!!</H1>
<p>
<a href='/strutsapp/html/form.htm'>Login</a>
</body>

The last is a simple logon table, which is a customized HTML, which can be placed under/strutsapp/html:

  • Form.htm
<body> 
   Please login:<p>
    <form name="userForm" action="/strutsapp/login.do" method="post">
   username <input type="text" name="username"/><br>
   password <input type="password" name="password"/><p>
        <input type="submit"/>
    </form>
</body>

In Struts, ActionForm is a part of the View component. It is an object-based table. The number of items in a table is automatically set to ActionForm, when there is no way to respond, you can ignore it. In ActionForm, you can fill in the table's single value, make some initial values, and perform basic information verification, actionForm can be used as a table to send data to the hidden area before the application. To some extent, it is a fireproof Area of the application, in ActionForm, you can avoid incorrect or insecure information entering the application program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.