无法超越ActionForm子类

时间:2011-03-18 12:51:59

标签: java validation struts struts-1

我有一个简单的网络应用程序:

它有一个简单的登录页面,用户名和密码。

我创建了一个名为LoginForm的ActionForm子类:

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

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
    {
        ActionErrors errors=new ActionErrors();

        System.out.println("In LoginForm");

        if(username.equals(""))
        {
            errors.add("username",new ActionMessage("loginerror.usernameEmpty"));
        }
        else if(password.equals(""))
        {
            errors.add("password",new ActionMessage("loginerror.passwordEmpty"));
        }

        return errors;
    }

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

}

一个Action子类:LoginAction

public class LoginAction extends Action
{
    public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServlet response)
    {
        LoginForm loginform=(LoginForm)form;

        String username=loginform.getUsername();
        String password=loginform.getPassword();

        System.out.println("Username="+username+" & Password="+password);

        if(username.equals("lokesh"))
        {
            ActionMessages errors=new ActionMessages();

            errors.add("username",new ActionMessage("loginerror.usernameInvalid"));

            saveErrors(request,errors);

            return mapping.getInputForward();

        }

        return mapping.findForward("success");
    }

}

struts动作映射是:

 <action-mappings>
        <action path="/login" type="occ.controller.LoginAction" name="LoginForm" scope="request" validate="true" input="/index.jsp"  >
            <forward name="success" path="/index.jsp" />
        </action>
    </action-mappings>

现在,ActionForm验证非常完美,但在LoginAction中完成的复杂验证无效。

该页面空白。

[我故意排除了进口]

1 个答案:

答案 0 :(得分:0)

您必须从org.apache.struts.validator.ValidatorForm(而不是ActionForm)扩展您的表单类(LoginForm)。我建议你使用内置的验证Struts功能。此网址可以帮助您:http://struts.apache.org/1.2.4/userGuide/dev_validator.html。使用它,您不必编写Java代码来验证常见(和复杂)规则。