在Struts2中,采用Validation Interceptor来完成校验支持,在struts-default.xml中,先调用conversionError interceptor进行type conversion操作,然后调用params preparevalidation interceptor完成对字段校验,interceptor的执行顺序是
如果在进行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 ..." 这样的信息转化成我们指定的信息,方法有几个:
将页面非String 类型的property转换成指定的类型,这样进行validation验证
参看http://struts.apache.org/2.0.11.1/docs/type-conversion.html
对于页面输入有意义的validator实际上只有requireddateint几个有意义,其它的意义都不大;同时由于要做单独的类型转换类,比较麻烦,不采用此种方式
又有几种方法
xwork.default.invalid.fieldvalue=数据格式不正确
invalid.fieldvalue.integerTest=数据格式不正确
例如在MyValidationAction.properties(与MyValidationAction放在同一目录中,而不是在classes下)中定义
invalid.fieldvalue.integerTest=数据格式不正确
注意:
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>ApplicationResources</param-value>
</context-param>
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 action ="myValidationAction" validate="true" >
<%@ 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>
<%@ 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>
<!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>
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;
}
}
<action method="execute" >
<result >validationOutput.jsp</result>
<result >validationInput.jsp</result>
</action>
http://struts.apache.org/2.x/docs/validation.html