无法超越 ActionForm 子类
我有一个简单的网络应用程序:
它有一个简单的登录页面,包含用户名和密码。
我创建了一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须从 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.