首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2中Validation跟Type Conversion

2012-08-27 
Struts2中Validation和Type Conversion在Struts2中,采用Validation Interceptor来完成校验支持,在struts-d

Struts2中Validation和Type Conversion

在Struts2中,采用Validation Interceptor来完成校验支持,在struts-default.xml中,先调用conversionError interceptor进行type conversion操作,然后调用params preparevalidation interceptor完成对字段校验,interceptor的执行顺序是

    型别转换 (type conversion) 获取参数信心(params ) 为验证等作准备(prepare) 参数验证 (validation)

??? 如果在进行type conversion时候发生错误,struts2对同样的字段就不会进行验证,会抛出异常,例如页面一输入字段希望进行是否为整数验证,由于页面输入值缺省都为String类型,因此会在后台抛出类似的错误:

ERROR (com.opensymphony.xwork2.interceptor.ParametersInterceptor:204) - ParametersInterceptor - [setParameters]: Unexpected Exception caught setting 'integerTest' on 'class com.mobilesoft.esales.webapp.action.MyValidationAction: Error setting expression 'integerTest' with value '[Ljava.lang.String;@11e170c'

??? 而在验证页面,会抛出如下的错误信息:

??? Invalid field value for field "integerTest"

?? 怎么把"Invalid field value for ..." 这样的信息转化成我们指定的信息,方法有几个:

1自定义转换类

将页面非String 类型的property转换成指定的类型,这样进行validation验证

? 参看http://struts.apache.org/2.0.11.1/docs/type-conversion.html

? 对于页面输入有意义的validator实际上只有requireddateint几个有意义,其它的意义都不大;同时由于要做单独的类型转换类,比较麻烦,不采用此种方式

2采用struts2的i18n支持

又有几种方法

2.1在资源文件中设定全局的xwork.default.invalid.fieldvalue 属性

xwork.default.invalid.fieldvalue=数据格式不正确

2.2在资源文件中设定invalid.fieldvalue.字段名称属性

invalid.fieldvalue.integerTest=数据格式不正确

2.3定义针对每一个Action的properties文件

例如在MyValidationAction.properties(与MyValidationAction放在同一目录中,而不是在classes下)中定义

invalid.fieldvalue.integerTest=数据格式不正确

注意:

目前在架构中,资源文件命名为ApplicationResources.properties,ApplicationResources_zh.properties,在web.xml中定义的:

<context-param>
??? <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
??? <param-value>ApplicationResources</param-value>
</context-param>

struts2缺省提供了一些type conversion 类,能够完成一些基本的转换操作:

String

boolean / Boolean

char / Character

int / Integer, float / Float, long / Long, double / Double

dates - uses the SHORT format for the Locale associated with the current request

arrays - assuming the individual strings can be coverted to the individual items

collections - if not object type can be determined, it is assumed to be a String and a new ArrayList is created

要完成客户端校验,注意在<s:form >上添加validate="true"

? <s:form action ="myValidationAction" validate="true" >

注意对资源文件进行native2ascii操作,或者用eclipse的PropEditor插件(http://propedit.sourceforge.jp/eclipse/updates)直接编辑

?

3例子3.1validationInput.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" errorPage="/error.jsp" pageEncoding="GBK" contentType="text/html;charset=GBK" %>

<html>
<head>
??? <title> Validation测试 </title>
??? <s:head />
</head>
<body>
??? <s:form action ="myValidationAction" validate="true" >???????????
??????? <s:textfield label ="String类型测试" />
??????? <s:textfield label ="Integer类型测试" />
??????? <s:textfield label ="email类型测试" />
??????? <s:submit />
??? </s:form>???
</body>
</html>

3.2validationOutput.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" errorPage="/error.jsp" pageEncoding="GBK" contentType="text/html;charset=GBK" %>

<html>
<head>
??? <title> Hello World </title>
</head>
<body>
??? String类型测试: <s:property value ="strTest" />?? <br/>
??? Integer类型测试: <s:property value ="integerTest" />? <br/>?
??? email类型测试: <s:property value ="emailTest" />??? <br/>
</body>
</html>

?

3.3MyValidationAction-validation.xml

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN"
??? "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
??? <field >
??????? <field-validator type="requiredstring">
??????????? <message>字段不能为空</message>
??????? </field-validator>
??? </field>

? <field >
<field-validator type="conversion">?
???? <message>conversion</message>?
</field-validator>
????? <field-validator type="required">
????????? <message>字段不能为空</message>
????? </field-validator>
????? <field-validator type="int">
????????? <param >6</param>
????????? <param >10</param>
????????? <message>值必须在 [ ${min}, ${max}], 当前的值是: ${integerTest}.</message>
????? </field-validator>
? </field>
??? <field >
????????? <field-validator type="required">
????????? <message>字段不能为空</message>
????? </field-validator>
??????? <field-validator type="email">
??????????? <message>字段必须为邮件地址</message>
??????? </field-validator>
??? </field>
</validators>

3.4MyValidationAction.java

package com.mobilesoft.esales.webapp.action;

public class MyValidationAction extends BaseAction {
??? private String strTest;
??? private Integer integerTest;
??? private String emailTest;
??? public String execute(){
??????? return SUCCESS;
??? }
??? public String getStrTest() {
??????? return strTest;
??? }
??? public void setStrTest(String strTest) {
??????? this.strTest = strTest;
??? }
??? public Integer getIntegerTest() {
??????? return integerTest;
??? }
??? public void setIntegerTest(Integer integerTest) {
??????? this.integerTest = integerTest;
??? }
??? public String getEmailTest() {
??????? return emailTest;
??? }
??? public void setEmailTest(String emailTest) {
??????? this.emailTest = emailTest;
??? }

}

?

3.5struts.xml

<action method="execute" >
??? <result >validationOutput.jsp</result>
??? <result >validationInput.jsp</result>
</action>????

4参考文档

http://struts.apache.org/2.x/docs/validation.html

http://struts.apache.org/2.0.11.1/docs/type-conversion.html

http://struts.apache.org/2.x/docs/localizing-output.html

热点排行