struts表单验证

在formBean的validate方法中添加错误验证如:

public void setPassword(String password) {
this.password = password;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
{
ActionErrors error=new ActionErrors();
if(this.username==null||this.username=="")
{
  error.add("username",new ActionMessage("login.error.username"));//username作为jsp页面查找的property-------(1)
}
if(this.password==null||this.password=="")
{
error.add("password",new ActionMessage("login.error.password"));//password作为jsp页面查找的property
}
   return error;
}
}

然后在页面中就可以根据key值查找到错误信息

如:

<html:errors property="username"/>就可以现在在资源文件中key值为ogin.error.username的属性值。

你可能感兴趣的:(struts表单验证)