struts2 adtion 方法的校验一

index.jsp

---------------------------------------------------------------------------------------

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">
        <title>index page</title>
    </head>
    <body>
        <font color="red">
        <s:fielderror></s:fielderror>
        </font>
        <br/><br/>
        doAdd method
        <form action="control/manager/employee_doAdd" method="POST">
            编号<input type="text" name="employee.empId"><br/>
            姓名<input type="text" name="employee.empName"><br/>
            手机<input type="text" name="employee.mobile"><br/>
            生日<input type="text" name="employee.birthday"><br/>
            <input type="submit" value="提交">
        </form>
        <br/><br/>
        doUpdate method
        <form action="control/manager/employee_doUpdate" method="POST">
            编号<input type="text" name="employee.empId"><br/>
            姓名<input type="text" name="employee.empName"><br/>
            手机<input type="text" name="employee.mobile"><br/>
            生日<input type="text" name="employee.birthday"><br/>
            <input type="submit" value="提交">
        </form>
    </body>
</html>

 

EmployeeAction.java

-----------------------------------------------------------------------------------------------

package org.taink.struts.action;

import java.util.regex.Pattern;

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.在视图中引用:<%@ taglib uri="/struts-tags" prefix="s" %>标签,
 * 并在页面中使用<s:fielderror></s:fielderror>标签
 *
 * 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 Employee employee;

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public String doAdd() {
        ActionContext.getContext().put("message", "添加成功");
        return "success";
    }

    public String doUpdate() {
        ActionContext.getContext().put("message", "更新成功");
        return "success";
    }

    /*
     * 1.此校验方法是自定义的 2.此校验方法只对当前的Action中的方法doAdd()方法进行校验
     */
    public void validateDoAdd() {
        if (null == this.employee.getEmpId()) {
            this.addFieldError("employee.empId", "编号不能为空");
        }
        if (null == this.employee.getEmpName()
                || "".equals(this.employee.getEmpName())) {
            this.addFieldError("employee.empName", "名称不能为空");
        }
        if (null == this.employee.getMobile()
                || "".equals(this.employee.getMobile())) {
            this.addFieldError("employee.mobile", "手机不能为空");
        } else {
            // 使用正则表达式匹配手机号
            if (!Pattern.compile("^1[358]\\d{9}$").matcher(
                    this.employee.getMobile()).matches()) {
                this.addFieldError("employee.mobile", "手机格式不正确");
            }
        }
        if (null == this.employee.getBirthday()) {
            this.addFieldError("employee.birthday", "生日不能为空");
        }
    }

    /*
     * 1.此校验方法是自定义的 2.此校验方法只对当前的Action中的方法doUpdate()方法进行校验
     */
    public void validateDoUpdate() {
        if (null == this.employee.getEmpId()) {
            this.addFieldError("employee.empId", "编号不能为空");
        }
        if (null == this.employee.getEmpName()
                || "".equals(this.employee.getEmpName())) {
            this.addFieldError("employee.empName", "名称不能为空");
        }
        if (null == this.employee.getMobile()
                || "".equals(this.employee.getMobile())) {
            this.addFieldError("employee.mobile", "手机不能为空");
        } else {
            if (!Pattern.compile("^1[358]\\d{9}$").matcher(
                    this.employee.getMobile()).matches()) {
                this.addFieldError("employee.mobile", "手机格式不正确");
            }
        }
        if (null == this.employee.getBirthday()) {
            this.addFieldError("employee.birthday", "生日不能为空");
        }
    }

    /*
     * 1.此校验方法是对父类ActionSupport 的validate 进行重写
     * 2.此校验方法对当前的Action中的所有方法都进行校验
     *
     */
    @Override
    public void validate() {
        if (null == this.employee.getEmpId()) {
            this.addFieldError("employee.empId", "编号不能为空");
        }
        if (null == this.employee.getEmpName()
                || "".equals(this.employee.getEmpName())) {
            this.addFieldError("employee.empName", "名称不能为空");
        }
        if (null == this.employee.getMobile()
                || "".equals(this.employee.getMobile())) {
            this.addFieldError("employee.mobile", "手机不能为空");
        } else {
            if (!Pattern.compile("^1[358]\\d{9}$").matcher(
                    this.employee.getMobile()).matches()) {
                this.addFieldError("employee.mobile", "手机格式不正确");
            }
        }
        if (null == this.employee.getBirthday()) {
            this.addFieldError("employee.birthday", "生日不能为空");
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值