45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:struts1.1中validation校验框架的应用有哪些?

struts1.1中validation校验框架的应用有哪些?

2016-09-04 04:39:32 来源:www.45fan.com 【

struts1.1中validation校验框架的应用有哪些?

校验框架加载:

在建立web模型时选中struts1.1中的validation,此事例以登录模块为例实现struts校验框架的脚本验证以及form验证,包括资源文件的国际化

1. struts配置文件

-------------------------------------structs-config.xml-----------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.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>

<form-property name="passWord" type="java.lang.String">

</form-property>

</form-bean>

<!--

此处为动态formBean,XML加载时加载此form,注意其typeDynaValidarorForm,若不使用动态Form也需继承此类.

-->

</form-beans>

<action-mappings>

<action input="/login.jsp" name="loginForm" path="/loginAction" scope="request" type="validate2.LoginAction" validate="true" >

<forward name="success" path="/wel.jsp">

</forward>

<!--

forword设置转向,应用于action中的mapping.findForword()

-->

</action>

</action-mappings>

<message-resources parameter="ApplicationResources" />

<!--

资源文件声明

-->

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">

<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />

</plug-in>

<!--

plug-in声明的校验框架的插件

-->

</struts-config>

------------------------------------web.xml------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<display-name>wp</display-name>

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>application</param-name>

<param-value>ApplicationResources</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet>

<servlet-name>debugjsp</servlet-name>

<description>Added by JBuilder to compile JSPs with debug info</description>

<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>

<init-param>

<param-name>classdebuginfo</param-name>

<param-value>true</param-value>

</init-param>

<load-on-startup>3</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>debugjsp</servlet-name>

<url-pattern>*.jsp</url-pattern>

</servlet-mapping>

<taglib>

<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-html.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-template.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>

</taglib>

</web-app>

---------------------validator-rules.xml校验框架自动生成无需修改在此不列出代码-----------

-----------------------validator.xml内容空白,需手动填写,用于声明校验方式及规则------------

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">

<form-validation>

<formset>

<form name="loginForm">

<field property="userName" depends="required">

<!--

property为校验的对象名称,depend为校验方式名称,required代表必填,

相应的校验方式名称在validator-rules中声明

-->

<arg0 key="userName.text"/>

<!--

处理信息参数,处理信息见资源文件,参数值也取自资源文件中键值为userName.text

的字符串

-->

</field>

<!--

-->

<field property="passWord" depends="required,maxlength">

<!--

多种校验用,分隔

-->

<arg0 key="passWord.text"/>

<arg1 key="${var:maxlength}" name="maxlength" resource="false"/>

<!--

key用表达式取某变量值,name为校验方式,resourse为是否从资源文件中取值

-->

<var>

<var-name>maxlength</var-name>

<var-value>6</var-value>

</var>

<!--

此处声明了上面用到的变量

-->

</field>

</form>

</formset>

</form-validation>

----------------------ApplicationResources.properties默认资源文件,复制validator-rules.xml中的注释即可,记录错误处理信息语句,可修改-------------------

errors.required={0} is required.

errors.minlength={0} can not be less than {1} characters.

errors.maxlength={0} lecky {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.

userName.text=userName

passWord.text=password

---------------------------------------ApplicationResources_zh.properties中文资源文件,将需求的信息由中文转置为ascii码存储-----------------------------------------------------------------------

errors.required={0} /u5fc5/u987b/u586b/u5199.

errors.minlength={0} /u4e0d/u80fd/u5c11/u4e8e {1} /u4e2a/u5b57.

errors.maxlength={0} /u4e0d/u80fd/u591a/u4e8e {1} /u4e2a/u5b57.

errors.invalid={0} /u5fc5/u987b/u5b9a/u4e49.

errors.byte={0} must be a byte.

errors.short={0} must be a short.

errors.integer={0} /u5fc5/u987b/u662f/u6574/u6570.

errors.long={0} must be a long.

errors.float={0} must be a float.

errors.double={0} must be a double.

errors.date={0} /u4e0d/u662f/u4e00/u4e2a/u65e5/u671f.

errors.range={0} is not in the range {1} through {2}.

errors.creditcard={0} is an invalid credit card number.

errors.email={0} /u4e0d/u662f/u5408/u6cd5/u7684Email.

userName.text=/u7528/u6237/u540d

passWord.text=/u5bc6/u7801

2. Action

此处与无验证框架的Aciton基本相同

-------------------------------LoginAction.java------------------------------------------------------------------

package validate2;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.Action;

import org.apache.struts.validator.DynaValidatorForm;

public class LoginAction extends Action {

public ActionForward execute(ActionMapping actionMapping,

ActionForm actionForm,

HttpServletRequest servletRequest,

HttpServletResponse servletResponse) {

DynaValidatorForm loginForm = (DynaValidatorForm) actionForm;

return actionMapping.findForward("success");

}

}

3. jsp页面

-------------------------------Login.jsp,登录页面,提交两个对象给LoginAction-------------------------

<%@ page contentType="text/html; charset=GB18030" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<html>

<head>

<title>

login

</title>

</head>

<body bgcolor="#ffffff">

<h1>

<font color="red">

<html:errors/>

<!--

此处为错误处理信息

-->

</font>

</h1>


本文地址:http://www.45fan.com/dnjc/71957.html
Tags: 校验 Validation struts1.1
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部