如何进行非字段验证?

所有配置和短路验证中一致,仅需修改  验证器公共TestAgeValidation-validation.xml、validation.jsp 、validationa.jsp 及struts.xml

示例如下

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~分割线,我有来了~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

项目结构:



修改后的公共验证器

<span style="font-size:18px;"><!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator 1.0.2//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">

<validators>

     <field name="age">
     	<!-- 类型转换验证 -->
     	<field-validator type="conversion" short-circuit="false" >
	               <message>类型转换错误</message>
	      </field-validator>
     	<!-- 
     		看看添加了短路验证 short-circuit="false" 
     		若类型转换错误的时候,还会不会进行下面的 int整型验证 
     	-->
     
         <field-validator type="int" short-circuit="false">
             <param name="min">1</param>
             <param name="max">130</param>
             <message key="error.int"></message>
         </field-validator>
     </field>
   
      <validator type="expression">
         <param name="expression"><![CDATA[password==password2]]></param>
         <message>密码不一致</message>
      </validator>


</validators></span>

新添加的 修改版的action错误消息提示

<span style="font-size:18px;"><#--
/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
-->
<#if (actionErrors?? && actionErrors?size > 0)>
	
	<#list actionErrors as error>
		<#if error?if_exists != "">
            <#if parameters.escape>${error!?html}<#else>${error!}</#if>
        </#if>
	</#list>
	</ul>
</#if></span>
修改后的struts.xml
仅修改了,若验证成后的转发到success.jsp ,其他没动

<?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>

		<!-- 配置全局的国际化资源文件 -->
	<constant name="struts.custom.i18n.resources" value="nihao"></constant>
	
	<package name="default" namespace="/" extends="struts-default">
	
		<!-- 这个就是起烂笔头的作用  -->
		<interceptors>
			<interceptor-stack name="baiduStack">
				<interceptor-ref name="paramsPrepareParamsStack">
					<param name="prepare.alwaysInvokePrepare">false</param>
				</interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="baiduStack"/>
	
		<action name="testAge1" class="com.baidu.ActionValidation.TestAgeValidation">
			<result>/success.jsp</result>
			
			<result name="input">/validation.jsp</result>
		</action>
		
		<action name="testAge2" class="com.baidu.ActionValidation.TestAgeValidation">
			<result>/success.jsp</result>
			
			<result name="input">/validationa.jsp</result>
		</action>	
			
		<!-- 中英文切换 是要使i18n 拦截器工作,因此是要使用一个action请求去调动其工作 -->
		<!-- index.jsp 通过action 转到 validation.jsp -->
		<action name="testVali" >
			<result>/validation.jsp</result>
		</action>
		<action name="testAge3" >
			<result>/validation.jsp</result>
		</action>	
		
	</package>

</struts>


页面: 仅添加了两个password 标签 和 一个s:actionerror 标签
validation.jsp

<span style="font-size:14px;"><%@ page language="java" 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>Insert title here</title>
</head>
<body>
		<br><br>
	<s:debug></s:debug>	
	<a href="testAge1.action?request_locale=zh_CN">中文</a>	
	 
	<a href="testAge1.action?request_locale=en_US">English</a>	
		<br><br>
	<a href="index.jsp">主页</a>	
	
	<center>
		
		<s:actionerror/>
		<s:form action="testAge1"   >
			<s:textfield name="age" key="age"></s:textfield>	
			<s:password  name="password" key="password"></s:password>
			<s:password  name="password2" key="password2"></s:password>
			
			<s:submit></s:submit>
		</s:form>
	
	</center>
	
</body>
</html></span>

validationa.jsp

<span style="font-size:14px;"><%@ page language="java" 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>Insert title here</title>
</head>
<body>
	<s:debug></s:debug>
		
	<center>
		
		<br><br>
		<br><br>
		
		<br><br>
		<s:actionerror/>
		
		<s:form action="testAge2"  >
			<s:textfield name="age" label="Age"></s:textfield>	
			<s:password  name="password" key="password"></s:password>
			<s:password  name="password2" key="password2"></s:password>
			
			<s:submit></s:submit>
		</s:form>
	
	</center>
	
</body>
</html></span>


效果:

password验证没有通过

如何进行非字段验证?_第1张图片


验证通过

如何进行非字段验证?_第2张图片

你可能感兴趣的:(如何进行非字段验证?)