上篇文章《使用struts2实现crud操作》中并未涉及数据有效性的验证,struts2也提供了一套验证框架,下面我们就体验一下struts的表单数据校验功能。
struts2校验框架的使用,并不需要在struts配置文件中配置,有点ror中约定替代配置的味道。还是使用上节的例子,TestAction在struts中的配置文件如下:
首先需要注意的是, 数据校验的文件也必须和该Action的存放位置一致,即放在” com.yangqiang.strutsdemo.web“下,xml的命名规则如下 Action类名-validation.xml(例如:TestAction-validation.xml),如果只是对某个action的数据校验,那么命名规则即为: Action类名 -别名-validation.xml (例如:TestAction-Store-validation.xml)即调用Store方法时进行数据检验[注意,这里使用action别名命名数据校验配置文件非常有用,我在系统中就只是使用了 TestAction-Store-validation.xml,在这种情况下我只给Store action配置了”input“ result。如果使用 TestAction-validation.xml,使用上面的struts.xml,打开调用List,Edit,Remove这三个action的页面时,就会报“没有定义input result”的错误]
其次,看看 TestAction-Store-validation.xml文件内容:
struts已经提供了很多校验器,大家可以查查资料看看。
上面的校验配置文件使用了资源文件,调用使用key。资源文件也使用规则命名,action类名_locale信息.properties(例如:TestAction_zh_CN.properties)
使用native2ascii -encoding utf-8 Test_cn.properties Test_zh_CN.properties命令进行编码转换
struts2校验框架的使用,并不需要在struts配置文件中配置,有点ror中约定替代配置的味道。还是使用上节的例子,TestAction在struts中的配置文件如下:
<
package
name
="struts_crud"
extends
="struts-default"
namespace
="/test"
>
< action name ="List" class ="com.yangqiang.strutsdemo.web.TestAction" method ="list" >
< result > list.jsp </ result >
</ action >
< action name ="Edit" class ="com.yangqiang.strutsdemo.web.TestAction" method ="load" >
< result > edit.jsp </ result >
</ action >
< action name ="Store" class ="com.yangqiang.strutsdemo.web.TestAction" method ="store" >
< result name ="input" type ="dispatcher" > edit.jsp </ result >
< result type ="redirect" > List.action </ result >
</ action >
< action name ="Remove" class ="com.yangqiang.strutsdemo.web.TestAction" method ="remove" >
< result type ="redirect" > List.action </ result >
</ action >
</ package >
< action name ="List" class ="com.yangqiang.strutsdemo.web.TestAction" method ="list" >
< result > list.jsp </ result >
</ action >
< action name ="Edit" class ="com.yangqiang.strutsdemo.web.TestAction" method ="load" >
< result > edit.jsp </ result >
</ action >
< action name ="Store" class ="com.yangqiang.strutsdemo.web.TestAction" method ="store" >
< result name ="input" type ="dispatcher" > edit.jsp </ result >
< result type ="redirect" > List.action </ result >
</ action >
< action name ="Remove" class ="com.yangqiang.strutsdemo.web.TestAction" method ="remove" >
< result type ="redirect" > List.action </ result >
</ action >
</ package >
首先需要注意的是, 数据校验的文件也必须和该Action的存放位置一致,即放在” com.yangqiang.strutsdemo.web“下,xml的命名规则如下 Action类名-validation.xml(例如:TestAction-validation.xml),如果只是对某个action的数据校验,那么命名规则即为: Action类名 -别名-validation.xml (例如:TestAction-Store-validation.xml)即调用Store方法时进行数据检验[注意,这里使用action别名命名数据校验配置文件非常有用,我在系统中就只是使用了 TestAction-Store-validation.xml,在这种情况下我只给Store action配置了”input“ result。如果使用 TestAction-validation.xml,使用上面的struts.xml,打开调用List,Edit,Remove这三个action的页面时,就会报“没有定义input result”的错误]
其次,看看 TestAction-Store-validation.xml文件内容:
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" >
< validators >
< field name ="test.name" >
< field-validator type ="requiredstring" >
< message key = "test.name" />
</ field-validator >
</ field >
< field name ="test.name" >
< field-validator type ="stringlength" >
< param name ="maxLength" > 100 </ param >
< param name ="minLength" > 5 </ param >
< param name ="trim" > true </ param >
< message > 必须5-100之内的字符长度 </ message >
</ field-validator >
</ field >
< field name ="test.author" >
< field-validator type ="requiredstring" >
< message > 作者不能为空! </ message >
</ field-validator >
</ field >
</ validators >
<! DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" >
< validators >
< field name ="test.name" >
< field-validator type ="requiredstring" >
< message key = "test.name" />
</ field-validator >
</ field >
< field name ="test.name" >
< field-validator type ="stringlength" >
< param name ="maxLength" > 100 </ param >
< param name ="minLength" > 5 </ param >
< param name ="trim" > true </ param >
< message > 必须5-100之内的字符长度 </ message >
</ field-validator >
</ field >
< field name ="test.author" >
< field-validator type ="requiredstring" >
< message > 作者不能为空! </ message >
</ field-validator >
</ field >
</ validators >
上面的校验配置文件使用了资源文件,调用使用key。资源文件也使用规则命名,action类名_locale信息.properties(例如:TestAction_zh_CN.properties)
test.name
=
书名不能为空!
test.author = 作者不能为空!
Constant.isRequired = 不能为空!
test.author = 作者不能为空!
Constant.isRequired = 不能为空!
使用native2ascii -encoding utf-8 Test_cn.properties Test_zh_CN.properties命令进行编码转换