Struts2-数据校验

1、数据校验分为2类,一类是前端的数据校验,一般通过js实现,一类是后端的数据校验。在对一些安全级别要求较高的项目都需要对其进行后端验证。struts2提供了两种后端校验方法(一种是编码实现,一种是检验框架实现)

2、如果要使用struts2的数据校验功能,action需要继承ActionSupport类。在该类中提供了一个validate方法,可以将验证规则写在该方法中,只有方法直行通过后,才会执行业务方法。

3、编码实现实例:

  • Jsp 页面:一定要加上<s:actionerror/>或者是<s:fielderror/>
<%--suppress ALL --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="S" uri="/struts-tags" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title>Struts2的数据处理</title>
  </head>
  <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>
</html>
  • Action 代码
package cn.ht.action;

import cn.ht.vo.User;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Date;

/**
 * @Classname UserAction
 * @Description TODO
 * @Date 2019-8-15 14:44
 * @Created by Administrator
 */
public class UserAction extends ActionSupport {
private String name;
    private String pwd;
    private int age;
    private Date birthday;
    @Override
    public String execute() throws Exception {
        System.out.println("execute");
        return SUCCESS;
    }
    public String register(){
        System.out.println("register");
        return SUCCESS;
    }
    public void validateRegister(){
        System.out.println("validate age");
        if(age>100||age<1){
            this.addActionError("年龄不合法");
        }
    }
    /*
     *   先执行validate方法,该方法通过后执行execute方法
     *   默认该方法是通过的
     *   在validate方法中,添加FieldError或者ActionError那么该方法将执行不能通过 并且放回的结果集为INPUT
     */
    public void validate() {
        System.out.println("validate");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
  • Struts.xml 配置文件
<package name="default" namespace="/" extends="struts-default">
        <action name="register" class="cn.ht.action.UserAction" method="register">
            <result>/success.jsp</result>
            <result name="input">/register.jsp</result>
        </action>
    </package>

4、struts2 的验证框架实例:

truts2 的验证框架
使用验证框架的步骤:在对应的 action 的包下添加一个验证框架的配置文件
该文件名为:actionName-validation.xml

  • Jsp 页面
<%--suppress ALL --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="S" uri="/struts-tags" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title>Struts2的数据处理</title>
  </head>
  <body>
  <s:fielderror></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>
</html>
  • Action 类
package cn.ht.action;

import cn.ht.vo.User;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Date;

/**
 * @Classname UserAction
 * @Description TODO
 * @Date 2019-8-15 14:44
 * @Created by Administrator
 */
public class UserAction extends ActionSupport {
    private String name;
    private String pwd;
    private int age;
    private Date birthday;
    @Override
    public String execute() throws Exception {
        System.out.println("execute");
        return SUCCESS;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
  • struts.xml 配置
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

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

 

  • UserAction-validation.xml 验证文件

 

<?xml version="1.0" encoding="UTF-8"?>
<!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="trim">true</param>
            <param name="maxLength">10</param>
            <param name="minLength">4</param>
            <message>用户名去掉 2 端空格后 长度为${minLength}到
                ${maxLength}</message>
        </field-validator>
    </field>
    <field name="age">
        <field-validator type="int">
            <param name="min">1</param>
            <param name="max">150</param>
            <message>年龄范围为 1~150</message>
        </field-validator>
    </field>
</validators>

 

posted @ 2019-08-15 17:28  贫血的吸血鬼  阅读(213)  评论(0编辑  收藏  举报