无法超越 ActionForm 子类

发布于 2024-10-24 00:39:59 字数 2385 浏览 3 评论 0原文

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

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

我创建了一个名为 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 中完成的复杂验证不起作用。

页面就变成空白了。

[我故意排除了进口]

I have a simple web app :

It has a simple login page taking username and password.

I have created an ActionForm subclass called LoginForm :

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

}

And a Action subclass : 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");
    }

}

The struts action mapping is :

 <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>

Now the ActionForm validation is going perfect but the complex validation done in LoginAction is not working.

The page just goes blank.

[I have excluded the imports deliberately]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-10-31 00:39:59

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

You must extend your form class (LoginForm) from org.apache.struts.validator.ValidatorForm (not ActionForm). I recommend you that use the built-in validation Struts feature. This url can help you: http://struts.apache.org/1.2.4/userGuide/dev_validator.html. Using that, you don't have to write Java code to validate common (and complex) rules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文