现在的位置: 首页 > 综合 > 正文

Struts validator 验证简介

2013年09月26日 ⁄ 综合 ⁄ 共 2661字 ⁄ 字号 评论关闭

   要用validator验证框架首先要在WEB-INF文件夹下建立一个名为validation.xml(必须为此名)的文件。 

validation.xml文件

 

<?xml version="1.0" encoding="UTF-8" ?>
 
<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
         
<form-validation>
 <global>
    <constant>
              <constant-name>phone</constant-name>
              <constant-value>^/d{8}$</constant-value>
       </constant>
 </global>
 
 <formset>
              <form name="userForm">
                     <field property="userName" depends="required">
                     <!-- field depends表示validator-rulers.xml中以定义好的方法 -->
                            <msg name="required" key="userName" resource="true" />
                            <!-- msg resource表示是否从properties文件中读取提示信息 -->
                     </field>
                     <field property="userPwd" depends="required,minlength">
                            <msg name="required" key="mima bu neng wei kong !"
                            resource="false" />
                            <msg name="minlength" key="chang du bu neng xiao yu 5!"
                            resource="false" />
                            <var>
                                   <var-name>minlength</var-name>
                                   <var-value>5</var-value>
                            </var>
                     </field>
             
              <field property="userPwd" depends="required">
                            <msg name="required" key="mima bu neng wei kong !"
                                   resource="false" />
                     </field>
              </form>
</formset>
</form-validation>       
 
 

Jsp页面

 

<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
       <head>
              <title>JSP for UserForm form</title>
       </head>
       <body>
<%--          <html:form action="/user">--%>
<%--                 userName : <html:text property="userName"/><html:errors property="userName"/><br/>--%>
<%--                 userPwd : <html:text property="userPwd"/><html:errors property="userPwd"/><br/>--%>
<%--                 <html:submit/><html:cancel/>--%>
<%--          </html:form>--%>
             
              <html:form action="/user">
                     userName : <html:text property="userName"/><html:errors property="userName"/><br/>
                     userPwd : <html:text property="userPwd"/><html:errors property="userPwd"/><br/>
                     <html:submit/><html:cancel />
              </html:form>
       </body>
</html>
 

 ApplicationResources.properties文件

 

# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
# Project ValidateTest
userName=yonghumingbunengweikong!

抱歉!评论已关闭.