1
重写 validator()方法
2 变量要有set/get方法,否则不能获取到值
3 如果出错了用
addFieldError();
4 默认在validator验证失败会跳转到input中的,所以记得加上input 例如
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="main" extends="struts-default" >
<action name="mainAction" class="com.YCHZ.jquery.action.LoginAction"
method="execute">
<result name="success">/success.jsp</result>
<result name="error">/index.jsp</result>
<result name="input">/error.jsp</result>
</action>
</package>
</struts>
5
举例
package com.YCHZ.jquery.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
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 String execute()
{
System.out.println("ok"+username+":"+password);
return SUCCESS;
}
@Override
public void validate() {
// TODO Auto-generated method stub
super.validate();
if("".equals(username) || username==null)
{
System.out.println("null");
addFieldError("username","username is null");
}
}
}
6 显示错误的页面
<s:fielderror fieldName="username"></s:fielderror>
7 结果