首页 > 代码库 > struts2框架xml验证

struts2框架xml验证

struts2验证分为3步:

  1、获取需要验证的信息,使用同名属性,提供getter,setter方法。然后框架使用反射将值自动注入。

  2、对信息进行验证,成功失败作出对应的选择。

 

xml验证和手动验证第一步都是一样的,第二步是使用xml配置文件完成的。

 

1、jsp前台页面:

  

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%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=http://www.mamicode.com/"<%=basePath%>">        <title>My JSP loginxml.jsp starting page</title>        <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href=http://www.mamicode.com/"styles.css">    -->  </head>    <body>    xml页面    <br/>    <s:fielderror/>    <s:form name="loginForm" method="post" namespace="/validate" action="validateXmlAction_login" theme="simple">        <table border="1">            <tr>                <td>用户名</td>                <td><s:textfield name="username" /></td>            </tr>            <tr>                <td>密码</td>                <td><s:password name="psw" /></td>            </tr>            <tr>                <td>&nbsp;</td>                <td><s:submit value=http://www.mamicode.com/"登录"/></td>            </tr>        </table>    </s:form>        <br/>        <a href=http://www.mamicode.com/"${pageContext.request.contextPath }/validate/validateXmlAction_test.do">test其他的方法</a>  </body></html>

 

书写验证文件xml:

  注意命名:命名规则为 Action类名+"-validation.xml"

   dtd文件在xwork-core包下的xwork.dtd文件中,自己选择版本。

<!DOCTYPE validators PUBLIC          "-//Apache Struts//XWork Validator 1.0.3//EN"          "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">

然后在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:指定action中要校验的属性            name:指定将被验证的表单字段的名字    -->    <field name="username">        <!--              field-validate:指定验证规则                type:指定验证规则的名称        -->            <field-validator type="requiredstring">            <!--                  param:向验证器传递的参数                    requiredstring验证器的参数是true,设置为true的作用是去空格            -->            <param name="trim">true</param>            <!--                  message:验证失败时,提示的错误信息            -->            <message><![CDATA[用户名不能为空!]]></message>        </field-validator>    </field>        <field name="psw">        <field-validator type="requiredstring">            <param name="trim">true</param>            <message><![CDATA[密码名不能为空!]]></message>        </field-validator>        <!-- 使用正则表达式验证规则来做 -->        <field-validator type="regex">            <param name="trim">true</param>            <param name="regexExpression"><![CDATA[^[0-9a-zA-Z]{6,12}]]></param>            <message><![CDATA[密码的长度必须在6至12之间]]></message>        </field-validator>    </field></validators>

 

注意:

  1、验证规则可以在/com/opensymphony/xwork2/validator/validators/default.xml下面可以找到。

  里面内容:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE validators PUBLIC        "-//Apache Struts//XWork Validator Definition 1.0//EN"        "http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd"><!-- START SNIPPET: validators-default --><validators>    <!--          validator:声明验证规则            name:验证规则的名称            class:验证规则对应的class路径    -->    <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>    <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>    <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>    <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>    <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>    <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>    <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>    <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>    <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>    <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>    <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>    <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>    <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>    <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>    <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>    <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/></validators><!--  END SNIPPET: validators-default -->

  2、给对应验证器的参数与验证器类中的属性一致,也是使用struts2反射注入值的方法将参数传递进去,所以如果不知道参数名如何指定,可以先查看验证器源码。

  3、<![CDATA[]]>可以保证内容不轻易被改变

 

我们使用第二个链接,发现这样做验证的是该Action中的所有的行为。如何验证单个行为呢?

  就是验证的xml的命名规则改变:

    Action类名+"-"+访问Action的指定Action名+"-validation.xml"

    例如ValidateXmlAction-validateXmlAction_login-validation.xml

    

struts2框架xml验证