struts2 adtion 方法的校验(二)基于xml 配置-taink-iteye技术网站

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";
  }
  }
  
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值