Struts2的验证机制

1.服务端验证
如果action继承ActionSupport,那么该action类将会继承ActionSupport的相关功能。如:验证功能。执行流程为
这里写图片描述
(validatexxx可有可无,成功执行为T,往下面执行,如果失败F,则直接跳过下面的步骤返回结果集为input)
注意:如果执行的是Action的execute方法,那么只会执行validate方法,如果执行的是自定义的Action的register方法,那么会先执行validateRegister方法——validate——register方法


案例
jsp页面一定要加上标签<s:actionerror>或者<s:fielderror>

 <body>
    <s:actionerror/>
    <form  action="register.action" method="post" >
    用户名:<input type="text"  name="name"/><br>
    密码:<input type="password"  name="pwd"/><br>
    年龄:<input type="text"  name="age"/><br>
    生日:<input type="text"  name="birthday"/><br>
    <input type="submit"  value="登录"/>
    </form>
  </body>

Action代码

import java.sql.Date;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport{
    private String  name;
    private  String pwd;
    private int age;
    private Date birthdaty ;
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return Action.SUCCESS;
    }
    public  String  register(){
        return Action.SUCCESS;
    }
    public void validateRegister(){
        if(age>100||age<1){
            this.addActionError("年龄不合法");
        }
    }
    @Override
    public void validate() {
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirthdaty() {
        return birthdaty;
    }
    public void setBirthdaty(Date birthdaty) {
        this.birthdaty = birthdaty;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

struts.xml

<struts>
    <package  name="default"  namespace="/"  extends="struts-default">
        <action  name="register"  class="cn.sxt.action.RegisterAction"  method="register">
            <result>/index.jsp</result>     
            <result  name="input">/register.jsp</result>        
        </action>
    </package>
</struts>

注意 web.xml配置过滤所有,因为这里我们直接访问jsp,而jsp用到了标签。如果不配置所有,jsp没有通过过滤器是无法启动标签的,会报错


除了上述,struts还提供了验证器,功能十分强大
在对应的action的包下添加一个验证框架的配置文件,名字为actionName-validation.xml


可以查看文档struts-2.3.32/docs/docs/index.html
里面选guides后如下
这里写图片描述
这里写图片描述


文档仅供参考,一般博主写Struts2不看文档,看源码来写。xwork-core-2.3.32.jar 下有个com.opensymphony.xwork2.validator.validators里面有个default.xml里面写了验证器的引用名。具体验证器的类看源码配置验证器属性(在default.xml同一个包下)。
jsp页面

 <body>
    <s:fielderror/>
    <form  action="register.action" method="post" >
    用户名:<input type="text"  name="name"/><br>
    密码:<input type="password"  name="pwd"/><br>
    年龄:<input type="text"  name="age"/><br>
    生日:<input type="text"  name="birthday"/><br>
    <input type="submit"  value="登录"/>
    </form>
  </body>

Action类

import java.sql.Date;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterValidateAction extends ActionSupport{
    private String  name;
    private  String pwd;
    private int age;
    private Date birthdaty ;
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return Action.SUCCESS;
    }

    @Override
    public void validate() {
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirthdaty() {
        return birthdaty;
    }
    public void setBirthdaty(Date birthdaty) {
        this.birthdaty = birthdaty;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package  name="default"  namespace="/"  extends="struts-default">
        <action  name="register"  class="cn.sxt.action.RegisterValidateAction" >
            <result>/index.jsp</result>     
            <result  name="input">/register.jsp</result>        
        </action>
    </package>
</struts>

RegisterValidateAction-validation.xml
头文件在文档里面拷,message需要在jsp配置这里写 <s:fielderror/> actionerror不可以
name是表单的数据名称

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
    <field name="name">
        <field-validator  type="requiredstring">
            <param  name="trim">true</param>
            <message>必须输入用户名字</message>
        </field-validator>
        <field-validator  type="stringlength">
            <param  name="minlength">10</param>
            <message>用户名至少为${minlength}</message>
        </field-validator>
    </field>
</validators>

需要什么样的验证就去找相关的类,文档中的例子

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
  <field name="bar">
      <field-validator type="required">
          <message>You must enter a value for bar.</message>
      </field-validator>
      <field-validator type="int">
          <param name="min">6</param>
          <param name="max">10</param>
          <message>bar must be between ${min} and ${max}, current value is ${bar}.</message>
      </field-validator>
  </field>
  <!--正则表达式-->
  <field name="bar2">
      <field-validator type="regex">
          <param name="expression">[0-9],[0-9]</param>
          <message>The value of bar2 must be in the format "x, y", where x and y are between 0 and 9</message>
     </field-validator>
  </field>
<!--日期验证-->
  <field name="date">
      <field-validator type="date">
          <param name="min">12/22/2002</param>
          <param name="max">12/25/2002</param>
          <message>The date must be between 12-22-2002 and 12-25-2002.</message>
      </field-validator>
  </field>
  <!--数字范围-->
  <field name="foo">
      <field-validator type="int">
          <param name="min">0</param>
          <param name="max">100</param>
          <message key="foo.range">Could not find foo.range!</message>
      </field-validator>
  </field>
  <!--可以用于密码确认,因为比较的是两个输入,不是某一个领域field,所以validator开头,取两个field的值并比较验证-->
  <validator type="expression">
      <param name="expression">foo lt bar </param>
      <message>Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.</message>
  </validator>
</validators>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值