调用ActionForm子类时的validate方法

时间:2013-08-16 17:58:02

标签: jsp struts-1

使用“雅加达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;
    }

}

2 个答案:

答案 0 :(得分:0)

是的,表单在这里表示 ActionForm实例

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

答案 1 :(得分:0)

ActionFormRequestProcessor方法中的processPopulate()填充。

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

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