- 浏览: 151204 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
hx0637:
楼主!小弟要面临面试了,能否指导下小弟?
面试 深圳一家公司的 -
kljismi:
你好,我现在正在开这项目的代码,但是我不明白@Privileg ...
权限管理模块分析 -
yzhw:
终于找到了
ImageSizer.java -
sunloveny:
国
struts国际化 -
jackotty:
谢谢楼主的分享
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
MessageResources.properties
MessageResources_zh_CN.properties
struts-config.java
validation.xml
validator-rules.xml
index.jsp
login.jsp
login2.jsp
login_success.jsp
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.
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.
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>
发表评论
-
drp_user
2009-01-11 15:10 1189采用DispathAction * 如果覆写DispathA ... -
编程式异常 -声明式异常
2009-01-11 11:28 14781、编程式异常 * 截获异常 * 创建相应的异常消息 * ... -
struts国际化
2009-01-07 23:18 66621、struts国际化的配置 * 在struts-confi ... -
了解Locale
2009-01-07 23:01 12721、了解缺省Locale是由操作系统决定的,Locale是由语 ... -
actionmapping
2009-01-07 22:48 14081、struts-config.xml文件中,每个<ac ... -
ActionForward的使用
2009-01-07 22:13 41991、理解全局和局部ActionForward的概念 2、red ... -
分步收集,ActionForm的scope=session
2009-01-07 21:58 1731StartAction.java package com.b ... -
ActionForm
2009-01-07 20:06 14971、动态ActionForm 动态ActionForm是为了 ... -
DispatchAction.java
2009-01-07 19:48 1076/* * $Id: DispatchAction.java ... -
模式匹配
2009-01-07 19:41 779UserAction.java package com. ... -
jstl标签库的配置
2009-01-07 18:51 3412jstl标签库的配置 * 将jstl.jar和standar ... -
1.2.9 RequestProcessor.java
2008-12-27 17:47 1301/* * $Id: RequestProcessor.ja ... -
1.2.9 ActionServlet.java
2008-12-27 17:43 1206/* * $Id: ActionServlet.java ... -
struts_login
2008-12-27 17:32 9071、配置struts * 拷贝struts lib下的所有j ... -
struts_trainnig_cal
2008-12-26 23:17 666CalAction.java package com.bj ... -
bjsxt test_servlet
2008-12-26 22:46 1126TestServlet.java package com. ...
相关推荐
### Struts Validator 验证框架详细讲解 #### 引言 在Java Web开发中,数据验证是确保应用程序安全性和用户体验的重要环节。Struts框架作为早期流行的MVC框架之一,提供了强大的验证机制——Struts Validator,它...
Struts Validator是一个强大的验证框架,它是Apache Struts框架的一部分,用于在Java Web应用程序中实现数据验证。这个框架帮助开发者在用户提交表单时确保输入的数据是合法、完整且符合业务规则的,从而提高应用...
Struts Validator是一个强大的验证框架,它是Apache Struts框架的一部分,用于在Java Web应用程序中实现数据输入验证。这个项目集成了验证规则,使得开发者能够轻松地确保用户提交的数据符合预期的格式和约束,从而...
Struts Validator是一个强大的验证框架,它是Apache Struts项目的一部分,专为Java Web应用程序设计,用于实现数据输入验证。Struts框架本身是一个MVC(模型-视图-控制器)架构,而Validator则是它的一个核心组件,...
### Struts1的Validator验证框架详解 在Java Web开发领域,Struts1作为一个经典且流行的MVC框架,提供了丰富的功能来构建动态Web应用。其中,Validator框架是Struts1用于客户端和服务端表单验证的强大工具,它能...
Struts的Validator验证框架是Java Web开发中常用的一个组件,主要用于处理用户输入的数据验证。它提供了强大的、可扩展的验证规则,使得开发者可以方便地在应用程序中实施数据校验,从而保证了数据的准确性和安全性...
Struts的Validator框架是Java Web开发中用于处理用户输入验证的一种强大的工具,它与MVC架构中的控制器层紧密结合,提供了一种便捷的方式来确保用户提交的数据符合预设的业务规则。这个框架大大简化了数据验证的过程...
1. Validator Framework:Validator Framework 是 Struts2 验证框架的核心组件,负责对用户输入的数据进行验证。Validator Framework 提供了一个通用的验证机制,可以对用户输入的数据进行验证。 2. Validators:...
Validator框架是Struts的一个重要组件,主要负责处理用户输入的数据验证,确保数据的完整性和正确性。在本文中,我们将深入探讨如何在Struts中使用Validator框架,并通过三个逐步进阶的实例来理解其工作原理。 首先...
Struts Validator框架是Struts框架的重要组成部分,用于实现客户端和服务器端的数据验证。自0.5版以来,Struts Validator就已集成在Struts中,起初作为开发人员包的一部分,随后其核心代码迁移到Jakarta Commons库和...
Struts Validator框架是Apache Struts框架的一个重要组成部分,由IBM公司提供支持,它主要用于Web应用程序中的数据验证。这个框架提供了一种结构化的方式来定义和执行客户端及服务器端的数据验证规则,确保用户输入...
此外,Struts1.x的验证框架不仅限于服务器端验证,也可以结合JavaScript进行客户端验证,以提高用户体验。客户端验证可以通过在JSP页面中引入Struts的JavaScript库,然后利用`<html:javascript>`标签生成相应的验证...
接下来,我们需要配置Validator插件,这是Struts1验证框架的核心。通过`<plug-in>`标签,指定类名为`org.apache.struts.validator.ValidatorPlugIn`,并设置`pathnames`属性指向`validator-rules.xml`和`validation....
Struts是Java Web开发中的一个流行MVC框架,它的核心组件之一是Validator,用于处理表单数据的验证。本文将详细介绍Struts验证器Validator的使用,包括基础配置、自定义验证器的创建,以及如何在Maven项目中管理和...
Struts Validator是Apache Struts框架的一个重要组成部分,用于在服务器端进行数据验证。它提供了一种灵活且可扩展的方式来确保用户提交的数据满足应用程序设定的规则和格式,从而提高应用程序的安全性和用户体验。...
Struts1.3 与1.2向比验证框架部分还是有所区别的,该例子通过一个简单的Email的校验,演示如何通过Struts Validate验证框架进行在页面中生成JavaScripte脚本,进行客户端校验。下完后记得评分,资源分就会回来,还能...
在Struts2中,验证框架是处理用户输入验证的关键部分,它确保了从客户端接收到的数据的质量和准确性。本示例将深入探讨Struts2验证框架的基本用法,特别适合初学者理解和掌握。 ### 一、Struts2验证框架概述 ...
Validator验证框架,STRUTS动态验证的规则与规范!