Struts2

本文详细介绍了Struts2框架中的验证机制,包括编程式验证、声明式验证及注解式验证的不同应用方式。重点讲解了声明式验证中的Non-FieldValidator与Field-Validator的区别与配置方法,并给出了具体的配置文件命名规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[b][size=large]Struts2 valiation:[/size][/b]
[b]分编程式验证、声明式验证、注解式验证。[/b]

[size=medium][b]声明式验证:[/b][/size]
[b][url]http://struts.apache.org/2.x/docs/validation.html[/url][/b]
[b][url]http://iehyou.iteye.com/blog/577214[/url][/b]
[url]http://hsw212.iteye.com/blog/300129[/url]

关于xxxAction-actionName-validation.xml:[quote]struts验证分为2种:Non-Field Validator 和 Field-Validator
[quote]There are two ways you can define validators in your -validation.xml file:
1 <validator>
2 <field-validator>
Keep the following in mind when using either syntax:
The <validator> element allows you to declare both types of validators (either a plain Validator a field-specific FieldValidator).
如:
下面是一个Non-Field Validator:

<validator type="expression>
<param name="expression">foo gt bar</param>
<message>foo must be great than bar.</message>
</validator>

下面是一个Field-Validator:

<validator type="required">
<param name="fieldName">bar</param>
<message>You must enter a value for bar.</message>
</validator>

The <field-validator> elements are basically the same as the <validator> elements except that they inherit the fieldName attribute from the enclosing <field> element. FieldValidators defined within a <field-validator> element will have their fieldName automatically filled with the value of the parent <field> element's fieldName attribute. The reason for this structure is to conveniently group the validators for a particular field under one element, otherwise the fieldName attribute would have to be repeated, over and over, for each individual <validator>.
[b]故结论:<field-validator>需和外围的<field>结合使用,并在外围<field>的name属性上指定field的值;他实现的效果和使用“<validator>嵌套<param name="fieldName">”的方式实现的Field-Validator是一模一样的!使用<field-validator>是为了对单个输入域的验证可以放在一起,方便书写查看归类而已。
在<field>中不可以使用Non-Field Validator。[/b]
[/quote]
[quote]验证器的执行先后:
Non Field Validators优先Field Validators,
Non Field Validators排在前面的先执行
Field Validators排在后面的先执行[color=red][b](我注:这句话是错误的,应该也是排在前面的先执行。详见:[url]http://struts.apache.org/2.x/docs/validation.html#Validation-ShortCircuitingValidator[/url])[/b][/color]
短路原则:
Non Field Validators最优先执行,如果某个字段效验失败,则改字段下的所有效验不会获得执行机会,不会影响其他字段的执行,Field Validators一样操作。
Non Field Validators不会影响Field Validators的执行。 [/quote]
expression效验器 求出ognl表达式的值。[b]为true通过验证[/b]
[/quote]
关于validation配置文件的命名:
两种方式:[quote]<actionClass>-validation.xml
<actionClass>-<actionAlias>-validation.xml[/quote][b]actionAlias指什么[/b]?指的就是action配置中<action>标签上name属性的名字。
比如有如下action配置:

<package name="upc.user" extends="mes-default"
namespace="/upc/user">
<action name="index" class="userAction" method="execute">
<result>userIndex.jsp</result>
</action>
<action name="save" class="userAction" method="save">
<result>/common/operationSuccess.jsp</result>
<result name="input">userEdit.jsp</result>
</action>
</package>
则其中的index和save就是actionAlias。
针对上例:
若validation配置文件名为UserAction-validation.xml,则UserAction的所有方法(这里有两个,execute方法和save方法)在执行前都会通过该校验文件做校验;
若validation配置文件名为UserAction-save-validation.xml,则只有UserAction的save方法在执行前会通过该校验文件做校验。


validation type required和requiredstring 的区别:
required validator
[url]http://struts.apache.org/2.0.14/docs/required-validator.html[/url]
requiredstring validator:
[url]http://struts.apache.org/2.0.14/docs/requiredstring-validator.html[/url]


[size=medium][b]编程式验证:[/b][/size]
[url]http://taink.iteye.com/blog/565690[/url](or:[url]http://51jsp.cn/a/struts/20100108/5545.html[/url])[quote]struts2 对action中的方法校验流程:
* 1.类型转换器对请求参数执行类型转换,并将转换后的值赋给action 中的属性
* 2.如果在执行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,conversionError 拦截器将异常信息添加到fieldErrors里,不管类型转换是否出现异常,都会进入第3步.
* 3.系统通过反射技术先调用action 中的validateXxxx()方法,Xxxx为方法名.
* 4.再调用action中的validate()方法.
* 5.经过上面4步,如果系统中的fieldErrors存在错误信息,
* (即存放错误的集合的size 大于0,系统自动将请求转发至名称为input 的视图.如果fieldErrors 没有任何的错误信息,系统将执行action 中处理方法)[/quote]
[url]http://ttitfly.iteye.com/blog/150304[/url][quote]struts2标签写的form表单
1. 只有FieldError级别错误才会自动显示出来,不再需要使用类似这样的标签:<s:fielderror/>
2. 验证出错的话,如果跳到form表单页面,那么会自动保留上次填入的表单信息
3. FieldError级别错误信息都会在每个Filed的上面显示
4. ActionError级别的错误会整体显示在一起,并且需要使用标签:<s:actionerror/>
5. 类型转换错误是属于FieldError级别的
6. ActionError是都放在List里的,而FieldError是放在Map里的
7. validateExecute invoke...
validate invoke..
或者
testValidate invoke...
validate invoke...
可以知道validate方法始终会被执行。 [/quote]


[b]Q:如果同时使用了编程式和声明式验证验证了同一东西,会先执行哪个?
A:经验证,会先执行编程式验证。[/b]例子如下:[quote]i18n文件actionClass.properties:

validation.requiredstring.steelgradeId=钢种ID为必填项

编程式save()方法验证:
public void validateSave() {
....
if(entity.getSteelgradeId()==null || "".equals(entity.getSteelgradeId()) {
this.addActionError("steelgradeId 非空!");
}
}

声明式save action(method配的就是save()方法)验证文件actionClass-save-validation.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" >
<validators>
<!-- the Non Field Validator is execute before than Field Validator.-->
<validator type="requiredstring">
<param name="fieldName">steelgradeId</param>
<param name="trim">true</param>
<message>${getText("validation.requiredstring.steelgradeId")}</message>
</validator>
</validators>

最终输出结果:
[img]http://dl.iteye.com/upload/attachment/333581/be6c3cd0-a8c5-35c2-976f-efae4fe5d940.png[/img][/quote]


关于struts2验证框架出现的“Invalid field value for field "xxxField"”:[quote]在xwork-2.0.4.jar\com\opensymphony\xwork2\xwork-messages.properties中定义的该验证信息;若未在struts.xml中指定struts.custom.i18n.resources,也未在actionName.properties中指定invalid.fieldvalue.xxxField=出错信息,则在出现类型转换错误的时候会使用xwork-messages.properties中的i18n出错配置,出现“Invalid field value for field "xxxField"”
[/quote][url]http://wing123.iteye.com/blog/415211[/url]
[url]http://www.java3z.com/cwbwebhome/article/article2/21084.html[/url]


[b][size=large]Interceptor[/size][/b]:
[url]http://www.blogjava.net/max/archive/2006/12/06/85925.html[/url]

为什么struts2控制台不打印出错信息:
[url]http://guangyao-yao.iteye.com/blog/812279[/url]
struts2配合log4j打印异常栈信息:
[url]http://tdcq.iteye.com/blog/706459[/url]


[b][size=large]Struts2 i18n:[/size][/b]
[url]http://wuaner.iteye.com/blog/643987[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值