`
xxp3369
  • 浏览: 151204 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

struts validator验证框架

阅读更多
struts validator验证框架

1、配置:
* 加入国际化配置在struts-config.xml文件中,如:
  <message-resources parameter="MessageResources" />
* 提供国际化资源文件
* 引入validator插件在struts-config.xml文件中,如:
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>
  * 提供validation.xml和validator_rules.xml文件,将此文件拷贝到WEB-INF下

2、validator服务器端验证
* 配置validation.xml文件

3、validator客户端验证(javascript)
) * 配置validation.xml文件
* 在jsp页面中包含< html:javascript>
* 对需要验证的表单定义onsubmit事件,其中事件名称为validate+ActionForm的名称,如:validateLoginForm
LoginAction.java


package com.bjsxt.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;

public class LoginAction extends Action {

    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
		DynaValidatorForm loginForm = (DynaValidatorForm)form;
		String username = (String)loginForm.get("username");
		String password = (String)loginForm.get("password");
		
		return mapping.findForward("success");
	}

}



MessageResources.properties


引用
prompt.username=User Name
prompt.password=User Password

errors.required={0},is required.



MessageResources_zh_CN.properties


引用
prompt.username=\u7528\u6237\u540d\u79f0
prompt.password=\u767b\u5f55\u5bc6\u7801

errors.required={0},\u662f\u5fc5\u987b\u7684.



struts-config.java

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
    <form-beans>
        <form-bean name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
            <form-property name="username" type="java.lang.String"/>
            <form-property name="password" type="java.lang.String"/>
       </form-bean>
    </form-beans>
	
	<action-mappings>
		<action path="/login1"
				type="com.bjsxt.struts.LoginAction"
				name="loginForm"
				scope="request"
				validate="true"
				input="/login1.jsp"
		>
			<forward name="success" path="/login_success.jsp"/>
		</action>
		
		<action path="/login2"
				type="com.bjsxt.struts.LoginAction"
				name="loginForm"
				scope="request"
				validate="true"
				input="/login2.jsp"
		>
			<forward name="success" path="/login_success.jsp"/>
		</action>
	</action-mappings>
	
    <message-resources parameter="MessageResources" />

  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
  </plug-in>
</struts-config>



validation.xml


<?xml version="1.0" encoding="ISO-8859-1" ?>

<!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>
    <formset>
        <form name="loginForm">
            <field property="username"  depends="required">
                    <arg key="prompt.username"/>
            </field>
            <field property="password" depends="required,mask">
                    <arg key="prompt.password"/>
                    <var>
                        <var-name>mask</var-name>
                        <var-value>^[0-9a-zA-Z]*$</var-value>
                    </var>
            </field>
        </form>
    </formset>
</form-validation>


validator-rules.xml


<!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">
<!--
  $Id: validator-rules.xml 159291 2005-03-28 20:19:29Z niallp $

   This file contains the default Struts Validator pluggable validator
   definitions.  It should be placed somewhere under /WEB-INF and
   referenced in the struts-config.xml under the plug-in element
   for the ValidatorPlugIn.

      <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
                                                  /WEB-INF/validation.xml"/>
      </plug-in>

   These are the default error messages associated with
   each validator defined in this file.  They should be
   added to your projects ApplicationResources.properties
   file or you can associate new ones by modifying the
   pluggable validators msg attributes in this file.

   # Struts Validator Error Messages
   errors.required={0} is required.
   errors.minlength={0} can not be less than {1} characters.
   errors.maxlength={0} can not be greater than {1} characters.
   errors.invalid={0} is invalid.

   errors.byte={0} must be a byte.
   errors.short={0} must be a short.
   errors.integer={0} must be an integer.
   errors.long={0} must be a long.
   errors.float={0} must be a float.
   errors.double={0} must be a double.

   errors.date={0} is not a date.
   errors.range={0} is not in the range {1} through {2}.
   errors.creditcard={0} is an invalid credit card number.
   errors.email={0} is an invalid e-mail address.

   Note: Starting in Struts 1.2.0 the default javascript definitions have
         been consolidated to commons-validator. The default can be overridden
         by supplying a <javascript> element with a CDATA section, just as
         in struts 1.1.

-->

<form-validation>

   <global>

      <validator name="required"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateRequired"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
                  msg="errors.required"/>

      <validator name="requiredif"
                 classname="org.apache.struts.validator.FieldChecks"
                 method="validateRequiredIf"
                 methodParams="java.lang.Object,
                               org.apache.commons.validator.ValidatorAction,
                               org.apache.commons.validator.Field,
                               org.apache.struts.action.ActionMessages,
                               org.apache.commons.validator.Validator,
                               javax.servlet.http.HttpServletRequest"
                 msg="errors.required"/>

      <validator name="validwhen"
          msg="errors.required"
                 classname="org.apache.struts.validator.validwhen.ValidWhen"
                 method="validateValidWhen"
                 methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"/>


      <validator name="minlength"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateMinLength"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.minlength"
           jsFunction="org.apache.commons.validator.javascript.validateMinLength"/>


      <validator name="maxlength"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateMaxLength"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.maxlength"
           jsFunction="org.apache.commons.validator.javascript.validateMaxLength"/>



      <validator name="mask"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateMask"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.invalid"/>


      <validator name="byte"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateByte"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.byte"
       jsFunctionName="ByteValidations"/>


      <validator name="short"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateShort"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.short"
       jsFunctionName="ShortValidations"/>


      <validator name="integer"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateInteger"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.integer"
       jsFunctionName="IntegerValidations"/>



      <validator name="long"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateLong"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.long"/>


      <validator name="float"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateFloat"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.float"
       jsFunctionName="FloatValidations"/>

      <validator name="double"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateDouble"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.double"/>


      <validator name="date"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateDate"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.date"
       jsFunctionName="DateValidations"/>


      <validator name="intRange"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateIntRange"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends="integer"
                  msg="errors.range"/>


      <validator name="floatRange"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateFloatRange"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends="float"
                  msg="errors.range"/>

      <validator name="doubleRange"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateDoubleRange"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends="double"
                  msg="errors.range"/>


      <validator name="creditCard"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateCreditCard"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.creditcard"/>


      <validator name="email"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateEmail"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.email"/>

      <validator name="url"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateUrl"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.url"/>

     <!--
       This simply allows struts to include the validateUtilities into a page, it should
       not be used as a validation rule.
     -->
     <validator name="includeJavaScriptUtilities"
            classname=""
               method=""
         methodParams=""
              depends=""
                  msg=""
           jsFunction="org.apache.commons.validator.javascript.validateUtilities"/>

   </global>

</form-validation>



index.jsp


<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>登录</title>
</head>
<body>
	<a href="login1.jsp">登录(validator框架服务器端验证)</a><br>
	<a href="login2.jsp">登录(validator框架客户端验证)(javascript)</a>
</body>
</html>



login.jsp


<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>    
<!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=GB18030">
<title>登录(validator框架服务器端验证)</title>
</head>
<body>
<h1>登录(validator框架服务器端验证)</h1>
<hr>
 <html:form action="login1.do">
   username : <html:text property="username"/><html:errors property="username"/><br/>
   password : <html:password property="password"/><html:errors property="password"/><br/>
   <html:submit/>
  </html:form>
</body>
</html>


login2.jsp


<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>    
<!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=GB18030">
<title>登录(validator框架客户端验证)</title>
</head>
<body>
<h1>登录(validator框架客户端验证(javascript))</h1>
<hr>
<html:javascript formName="loginForm"/>
 <html:form action="login2.do" onsubmit="return validateLoginForm(this)">
  	  username : <html:text property="username"/><br/>
 	  password : <html:password property="password"/><br>
   <html:submit/>
  </html:form>
</body>
</html>


login_success.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>登录成功</title>
</head>
<body>
登录成功
</body>
</html>
分享到:
评论
1 楼 jackotty 2009-03-06  
谢谢楼主的分享

相关推荐

    Struts Validator验证框架详细讲解.txt

    ### Struts Validator 验证框架详细讲解 #### 引言 在Java Web开发中,数据验证是确保应用程序安全性和用户体验的重要环节。Struts框架作为早期流行的MVC框架之一,提供了强大的验证机制——Struts Validator,它...

    Struts validator验证框架

    Struts Validator是一个强大的验证框架,它是Apache Struts框架的一部分,用于在Java Web应用程序中实现数据验证。这个框架帮助开发者在用户提交表单时确保输入的数据是合法、完整且符合业务规则的,从而提高应用...

    struts validator验证框架项目

    Struts Validator是一个强大的验证框架,它是Apache Struts框架的一部分,用于在Java Web应用程序中实现数据输入验证。这个项目集成了验证规则,使得开发者能够轻松地确保用户提交的数据符合预期的格式和约束,从而...

    struts validator验证框架例子

    Struts Validator是一个强大的验证框架,它是Apache Struts项目的一部分,专为Java Web应用程序设计,用于实现数据输入验证。Struts框架本身是一个MVC(模型-视图-控制器)架构,而Validator则是它的一个核心组件,...

    Struts1的validator验证框架

    ### Struts1的Validator验证框架详解 在Java Web开发领域,Struts1作为一个经典且流行的MVC框架,提供了丰富的功能来构建动态Web应用。其中,Validator框架是Struts1用于客户端和服务端表单验证的强大工具,它能...

    Struts的Validator验证框架使用教程

    Struts的Validator验证框架是Java Web开发中常用的一个组件,主要用于处理用户输入的数据验证。它提供了强大的、可扩展的验证规则,使得开发者可以方便地在应用程序中实施数据校验,从而保证了数据的准确性和安全性...

    struts 的validator框架验证

    Struts的Validator框架是Java Web开发中用于处理用户输入验证的一种强大的工具,它与MVC架构中的控制器层紧密结合,提供了一种便捷的方式来确保用户提交的数据符合预设的业务规则。这个框架大大简化了数据验证的过程...

    struts2验证框架

    1. Validator Framework:Validator Framework 是 Struts2 验证框架的核心组件,负责对用户输入的数据进行验证。Validator Framework 提供了一个通用的验证机制,可以对用户输入的数据进行验证。 2. Validators:...

    struts中使用validator验证框架

    Validator框架是Struts的一个重要组件,主要负责处理用户输入的数据验证,确保数据的完整性和正确性。在本文中,我们将深入探讨如何在Struts中使用Validator框架,并通过三个逐步进阶的实例来理解其工作原理。 首先...

    Struts Validator验证器使用指南

    Struts Validator框架是Struts框架的重要组成部分,用于实现客户端和服务器端的数据验证。自0.5版以来,Struts Validator就已集成在Struts中,起初作为开发人员包的一部分,随后其核心代码迁移到Jakarta Commons库和...

    IBM 的 Struts validator框架

    Struts Validator框架是Apache Struts框架的一个重要组成部分,由IBM公司提供支持,它主要用于Web应用程序中的数据验证。这个框架提供了一种结构化的方式来定义和执行客户端及服务器端的数据验证规则,确保用户输入...

    struts1.x使用common-validator验证框架

    此外,Struts1.x的验证框架不仅限于服务器端验证,也可以结合JavaScript进行客户端验证,以提高用户体验。客户端验证可以通过在JSP页面中引入Struts的JavaScript库,然后利用`&lt;html:javascript&gt;`标签生成相应的验证...

    struts1验证框架相当好的经典例子

    接下来,我们需要配置Validator插件,这是Struts1验证框架的核心。通过`&lt;plug-in&gt;`标签,指定类名为`org.apache.struts.validator.ValidatorPlugIn`,并设置`pathnames`属性指向`validator-rules.xml`和`validation....

    struts验证器validator使用,以及自定义验证器

    Struts是Java Web开发中的一个流行MVC框架,它的核心组件之一是Validator,用于处理表单数据的验证。本文将详细介绍Struts验证器Validator的使用,包括基础配置、自定义验证器的创建,以及如何在Maven项目中管理和...

    struts validator验证实例

    Struts Validator是Apache Struts框架的一个重要组成部分,用于在服务器端进行数据验证。它提供了一种灵活且可扩展的方式来确保用户提交的数据满足应用程序设定的规则和格式,从而提高应用程序的安全性和用户体验。...

    Struts1.3 Validator 验证框架校验Email 例子

    Struts1.3 与1.2向比验证框架部分还是有所区别的,该例子通过一个简单的Email的校验,演示如何通过Struts Validate验证框架进行在页面中生成JavaScripte脚本,进行客户端校验。下完后记得评分,资源分就会回来,还能...

    struts2验证框架简单示例

    在Struts2中,验证框架是处理用户输入验证的关键部分,它确保了从客户端接收到的数据的质量和准确性。本示例将深入探讨Struts2验证框架的基本用法,特别适合初学者理解和掌握。 ### 一、Struts2验证框架概述 ...

    Validator验证框架

    Validator验证框架,STRUTS动态验证的规则与规范!

Global site tag (gtag.js) - Google Analytics