struts2 adtion 方法的校验(二)基于xml 配置-taink-iteye技术网站
2011年07月01日
EmployeeAction-employee_doAdd-validation.xml -------------------------------------------------- --------------------------------------------------- ----------
true
编号不能为空
true
编号为数字
姓名不能为空
手机号不能为空
手机号格式错误
EmployeeAction-validation.xml
-------------------------------------------------- ------------------------------------------
true
编号不能为空
true
编号为数字
姓名不能为空
手机号不能为空
手机号格式错误
struts.xml
-------------------------------------------------- ---------------------------------------------------
/index.jsp
/WEB-INF/page/message.jsp
EmployeeAction.java
-------------------------------------------------- --------------------------------------------------- -------
package org.taink.struts.action;
import org.taink.entity.Employee;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* struts2 对action中的方法进行校验的分类: 1.采用手工编写代码方式实现 a.对action 中的所有方式进行校验,
* 即重写父类ActionSupport中的validate()方法 b.只对action 中指定方式进行校验,需要自定义校验方式.
*
* 2.基于XML 配置方式实现
*
* struts2 对action中的方法校验实现方式: 1.需要校验的action
* 需要继承ActionSupport类,对action中的所有方法进行校验, 就重写父类ActionSupport中的validate()方法
* ;只对action 中指定方式进行校验,需要自定义校验方式. 2.在视图中引用:标签, 并在页面中使用标签
*
* struts2 对action中的方法校验流程: 1.类型转换器对请求参数执行类型转换,并将转换后的值赋给action 中的属性
* 2.如果在执行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,conversionError
* 拦截器将异常信息添加到fieldErrors里,不管类型转换是否出现异常,都会进入第3步. 3.系统通过反射技术先调用action
* 中的validateXxxx()方法,Xxxx为方法名. 4.再调用action中的validate()方法.
* 5.经过上面4步,如果系统中的fieldErrors存在错误信息, (即存放错误的集合的size 大于0,系统自动将请求转发至名称为input
* 的视图.如果fieldErrors 没有任何的错误信息,系统将执行action 中处理方法)
*
* @author taink
*
*/
public class EmployeeAction extends ActionSupport {
private static final long serialVersionUID = 6892944822771610653L;
private Integer empId;
private String empName;
private String mobile;
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String doAdd() {
ActionContext.getContext().put("message", "添加成功");
return "success";
}
public String doUpdate() {
ActionContext.getContext().put("message", "更新成功");
return "success";
}
}
2011年07月01日
EmployeeAction-employee_doAdd-validation.xml -------------------------------------------------- --------------------------------------------------- ----------
true
编号不能为空
true
编号为数字
姓名不能为空
手机号不能为空
手机号格式错误
EmployeeAction-validation.xml
-------------------------------------------------- ------------------------------------------
true
编号不能为空
true
编号为数字
姓名不能为空
手机号不能为空
手机号格式错误
struts.xml
-------------------------------------------------- ---------------------------------------------------
/index.jsp
/WEB-INF/page/message.jsp
EmployeeAction.java
-------------------------------------------------- --------------------------------------------------- -------
package org.taink.struts.action;
import org.taink.entity.Employee;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* struts2 对action中的方法进行校验的分类: 1.采用手工编写代码方式实现 a.对action 中的所有方式进行校验,
* 即重写父类ActionSupport中的validate()方法 b.只对action 中指定方式进行校验,需要自定义校验方式.
*
* 2.基于XML 配置方式实现
*
* struts2 对action中的方法校验实现方式: 1.需要校验的action
* 需要继承ActionSupport类,对action中的所有方法进行校验, 就重写父类ActionSupport中的validate()方法
* ;只对action 中指定方式进行校验,需要自定义校验方式. 2.在视图中引用:标签, 并在页面中使用标签
*
* struts2 对action中的方法校验流程: 1.类型转换器对请求参数执行类型转换,并将转换后的值赋给action 中的属性
* 2.如果在执行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,conversionError
* 拦截器将异常信息添加到fieldErrors里,不管类型转换是否出现异常,都会进入第3步. 3.系统通过反射技术先调用action
* 中的validateXxxx()方法,Xxxx为方法名. 4.再调用action中的validate()方法.
* 5.经过上面4步,如果系统中的fieldErrors存在错误信息, (即存放错误的集合的size 大于0,系统自动将请求转发至名称为input
* 的视图.如果fieldErrors 没有任何的错误信息,系统将执行action 中处理方法)
*
* @author taink
*
*/
public class EmployeeAction extends ActionSupport {
private static final long serialVersionUID = 6892944822771610653L;
private Integer empId;
private String empName;
private String mobile;
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String doAdd() {
ActionContext.getContext().put("message", "添加成功");
return "success";
}
public String doUpdate() {
ActionContext.getContext().put("message", "更新成功");
return "success";
}
}