Struts2内置校验器——完整实例代码


一、校验器的配置风格

1、字段校验器:

<field name="被校验的字段">

       <field-validator type="校验器名">

             <!--此处需要为不同校验器指定数量不等的校验规则-->

              <param name="参数名">参数值</param>

                  ....................

               <!--校验失败后的提示信息,其中key指定国际化信息的key-->

                 <message>校验失败后的提示信息</message>

                 <!--校验失败后的提示信息:建议用getText("I18Nkey"),否则可能出现Freemarker template Error-->

       </field-vallidator>

        <!-- 如果校验字段满足多个规则,下面可以配置多个校验器-->

</field>

 

2、非字段校验器(本实例所使用):

<validator type="校验器名">

          <param name="fieldName">需要被校验的字段</param>

          <!--此处需要为不同校验器指定数量不等的校验规则-->          
    <param name="参数名">参数值</param>

         <message>校验失败后的提示信息</message>

</validator>

两种配置只是风格上的不同,没有本质区别。

二、实例

  本注册项目中使用了4个内置校验器。项目只有一个注册页面(register2.jsp),对应的业务控制器(Register2Action.java)。如果输入的数据经内置验证器验证成功,跳转到success2.jsp。

 

实例步骤如下:

注意:

Register2Action.java和Register2Action-validation.xml在同一个包validatorAction内

register2.jsp和success1.jsp在WebContent下(我用的是Eclipse)同一个文件夹validate内

1、web.xml配置FilterDispatcher

复制代码
<?xml version="1.0" encoding="UTF-8"?>  
<web-app  version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  

  
    <filter>  
        <!-- 配置Struts2核心Filter的名字 -->  
        <filter-name>struts2</filter-name>  
        <!-- 配置Struts2核心Filter的实现类 -->  
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
    </filter>  
    <!-- 配置Filter拦截的URL -->  
    <filter-mapping>  
        <!-- 配置Struts2的核心FilterDispatcher拦截所有用户请求 -->  
        <filter-name>struts2</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  

</web-app>  
复制代码

 

2、编写视图组件(JSP页面)

(1)注册页面(register2.jsp)

复制代码
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户注册页面</title>
</head>
<body>
    <center>
        请输入信息:
        <hr>
        <s:form action="register2" method="post">
            <table border="1">
                <tr>
                    <td>
                        <s:textfield name="userName" label="姓名" size="16" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <s:password name="userPassword" label="密码" size="18" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <s:password name="ruserPassword" label="再次输入密码" size="18" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <s:textfield name="userAge" label="年龄" size="16" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <s:textfield name="userTelephone" label="电话" size="16" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <s:textfield name="userEmail" label="邮箱" size="16" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <s:submit value="提交" />
                    </td>
                </tr>
            </table>
        </s:form>
    </center>
</body>
</html>

复制代码

 

(2)验证成功页面(success2.jsp)

复制代码
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>校验成功</title>
</head>
<body>
    校验通过,用户信息如下:
    <hr>
    姓名:<s:property value="userName" />
    <br>
    密码:<s:property value="userPassword" />
    <br>
    年龄:<s:property value="userAge" />
    <br>
    电话:<s:property value="userTelephone" />
    <br>
    邮箱:<s:property value="userEmail" />
</body>
</html>
复制代码

 

3、编写业务控制器(Register2Action.java)

复制代码
package validatorAction;

import com.opensymphony.xwork2.ActionSupport;

public class Register2Action extends ActionSupport{

    private String userName;
    private String userPassword;
    private String ruserPassword;
    private int userTelephone;
    private int userAge;
    private String userEmail;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserPassword() {
        return userPassword;
    }
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    public String getRuserPassword() {
        return ruserPassword;
    }
    public void setRuserPassword(String ruserPassword) {
        this.ruserPassword = ruserPassword;
    }
    public int getUserTelephone() {
        return userTelephone;
    }
    public void setUserTelephone(int userTelephone) {
        this.userTelephone = userTelephone;
    }
    public int getUserAge() {
        return userAge;
    }
    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }
    public String getUserEmail() {
        return userEmail;
    }
    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }
    
    
    public String execute(){
        return "success";
    }
    
    
}
复制代码

 

4、配置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="intercept" extends="struts-default">
        <action name="register2" class="validatorAction.Register2Action">
            <result name="input">/validate/register2.jsp</result>
            <result name="success">/validate/success2.jsp</result>
        </action>
    </package>
</struts>
复制代码

 

5、内置验证器的验证文件(Register2Action-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>
    <!-- 字符串长度校验器 ,校验userName属性-->
      <validator type="stringlength">
          <param name="fieldName">userName</param>
          <param name="maxLength">16</param>
          <param name="minLength">6</param>
          <message>姓名长度为${minLength}到${maxLength}个字符!</message>
      </validator>
      
      <!-- 字符串长度校验器 ,校验userPassword属性-->
      <validator type="stringlength">
          <param name="fieldName">userPassword</param>
          <param name="maxLength">16</param>
          <param name="minLength">6</param>
          <message>密码长度为${minLength}到${maxLength}个字符!</message>
      </validator>
      
      <!-- 字段表达式校验器-->
      <validator type="fieldexpression">
          <param name="fieldName">userPassword</param>
          <param name="expression">
              <!-- 验证两次输入的密码是否相同 -->
              <![CDATA[userPassword==ruserPassword]]>
          </param>
          <message>两次密码不一致!</message>
      </validator>
      
      <!-- 整数校验器-->
      <validator type="int">
          <param name="fieldName">userAge</param>
          <param name="min">1</param>
          <param name="max">130</param>
          <message>年龄必须在${min}到${max}之间!</message>
      </validator>
      
      <!-- 整数校验器 -->
      <validator type="int">
          <param name="fieldName">userTelephone</param>
          <param name="min">22222222</param>
          <param name="max">99999999</param>
          <message>电话必须在${min}到${max}之间的八位号码!</message>
      </validator>
      
  <!-- 邮件地址校验器-->
      <validator type="email">
          <param name="fieldName">userEmail</param>
          <message>请使用正确的邮件格式!</message>
      </validator>
      
</validators>
复制代码

如果验证失败:

注意文件命名规则(X-validation.xml)

检查stringlength等属性的大小写,还有字段是否和前面匹配;

查看struts-2.3.28.1\src\xwork-core\src\main\resources\xwork-validator-1.0.3.dtd文件,拷贝前4行代码,因版本而异。

等等……

分类:  Struts2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值