0

使用“Jakarta Struts for Dummies”一书学习 Struts 1.1,同时阅读ActionForm它说

validate method: Is called after the `ActionServlet` populates the
form with the request properties and before the form is passed to a particular
`Action` class for processing

实际上,我们提交带有属性(比如用户名或密码)的表单,这些都在请求中填充。但这里是说ActionServlet填充表格?

我认为这里的形式手段ActionForm或其子类仪式,如果我错了,请纠正我。

这是表格:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- JSTL tag libs --%>
<%@ taglib prefix="fmt" uri="/WEB-INF/fmt.tld" %>
<%-- Struts provided Taglibs --%>
<%@ taglib prefix="html" uri="/WEB-INF/struts-html-el.tld" %>
<html:html locale="true"/>
<head>
    <fmt:setBundle basename="ApplicationResources" />
    <title><fmt:message key="login.title"/></title>
</head>
<body>
<html:errors property="login"/>
<html:form action="login.do" focus="userName">
    <table align="center">
        <tr align="center">
            <td><H1><fmt:message key="login.message"/></H1></td>
        </tr>
        <tr align="center">
            <td>
                <table align="center">
                    <tr>
                        <td align="right">
                            <fmt:message key="login.username"/>
                        </td>
                        <td align="left">
                            <html:text  property="userName" 
                                        size="15" 
                                        maxlength="15" />
                            <html:errors property="userName" />
                        </td>
                    </tr>   
                    <tr>
                        <td align="right">
                            <fmt:message key="login.password"/>
                        </td>
                        <td align="left">
                            <html:password  property="password" 
                                            size="15" 
                                            maxlength="15" 
                                            redisplay="false"/>
                            <html:errors property="password" />
                        </td>
                    </tr>   
                    <tr>
                        <td colspan="2" align="center">
                            <html:submit>
                                <fmt:message key="login.button.signon"/>
                            </html:submit>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</html:form>
</body>
</html>

这是LoginForm

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * @author kiran
 *
 */
public class LoginForm extends ActionForm 
{
    private String userName;
    private String password;
    int count=0;

    /**
     * Resets data fields to initial values on loginform
     * @param mapping
     * @param request
     */
    public void reset(ActionMapping mapping, HttpServletRequest request)
    {
        password = "";
        userName = "";
        count++;
        System.out.println("reset method is called"+count+ " time/s");
    }


    /**
     * Performs validation of data on loginform
     * @param mapping
     * @param request
     * @return ActionErrors
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        System.out.println("validate method is called"+count+" time/s");
        System.out.println(getUserName());
        ActionErrors errors = new ActionErrors();

        if((userName == null) || (userName.length() < 1))
            errors.add("userName", new ActionError("error.username.required"));
        if((password == null) || (password.length() < 1))
            errors.add("password", new ActionError("error.password.required"));

        return errors;

    }
    /**
     * @return String
     */
    public String getPassword() {
        return password;
    }

    /**
     * @return String
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param string
     */
    public void setPassword(String string) {
        password = string;
    }

    /**
     * @param string
     */
    public void setUserName(String string) {
        userName = string;
    }

}
4

2 回答 2

0

是的,这里的表单是指ActionForm 实例

你为什么要学习一个正式退役的框架(Struts 1),因为它已经过时并且不再维护了?为什么你要学习这个过时框架的 1.1 版本,而它的最新版本是 1.3.x?自 2004 年以来,不应再使用 Struts 1.1!9年前!

于 2013-08-16T18:01:21.283 回答
0

由方法中的ActionForm填充。RequestProcessorprocessPopulate()

这是一个代码片段,显示了操作表单的填充位置:

    // Process any ActionForm bean related to this request
    ActionForm form = processActionForm(request, response, mapping);

    processPopulate(request, response, form, mapping);

    // Validate any fields of the ActionForm bean, if applicable
    try {
        if (!processValidate(request, response, form, mapping)) {
            return;
        }
    } catch (InvalidCancelException e) {
        ActionForward forward = processException(request, response, e, form, mapping);
        processForwardConfig(request, response, forward);
        return;
    } catch (IOException e) {
        throw e;
    } catch (ServletException e) {
        throw e;
    }
于 2013-08-16T18:10:33.213 回答