Struts(五)校验框架
1.Struts2的校验框架(有效的xml文件)。具体来说分为字段优先校验器与校验器优先校验器。
2.字段优先校验器
1)RegisterAction-validation.xml(前面为action名)//例如,action包内存放该文件
<?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> <field name="username"> <field-validator type="requiredstring"> <message>username is blank!</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">4</param> <param name="maxLength">6</param> <message>4-6</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message>password is blank</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">4</param> <param name="maxLength">6</param> <message>${minLength}-6</message> </field-validator> </field> <field name="age"> <field-validator type="required"> <message>age is blank</message> </field-validator> <field-validator type="int"> <param name="min">10</param> <param name="max">50</param> <message>10-50</message> </field-validator> </field> <field name="brithday"> <field-validator type="date"> <param name="min">2005-1-1</param> <param name="max">2007-7-31</param> <message>Birthday must be within ${min} and ${max}</message> </field-validator> </field> </validators>
name就是在设置校验时候的type。也是struts里面内置的校验器。
<validators> <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/> <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/> <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/> <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/> <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/> <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/> <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/> <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/> <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/> <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/> <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/> <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/> <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/> <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/> </validators>
设置trim参数,可以允许输入空格!
3.校验器优先校验
<validator type="requiredstring"> <param name="fieldName">username</param> <message>must</message> </validator> <validator type="stringlength"> <param name="fieldName">username</param> <param name="minLength">4</param> <param name="maxLength">6</param> <message>4-6</message> </validator>
4.两种比较器可以混用
5.Struts2校验执行的先后顺序:
1)首先执行校验框架(xml文件)
2)执行自定义方法的校验方法(validateMyExecute)
3)执行validate方法
6.国际化
可以设置message属性里面的key,作用是当你浏览器的首选是那个语言,那么校验的信息会显示那种语言。例如,中文的话,则会显示:用户名不符合!
对于国际化的资源文件,其命名规则是:package_语言名(小写)_国家名(大写)。
package_en_US.properties(英文的,key值=message的具体消息)
username=username is not !
package_zh_CN.properties
username=\u7528\u6237\u540D\u65E0\u6548\uFF01
i18n=国际化,
1)i18n.java
package com.iiule.i18n; import java.util.Locale; public class testI18n { public static void main(String[] args) { Locale[] locales = Locale.getAvailableLocales(); for(Locale locale:locales) { System.out.println(locale.getDisplayCountry()+":"+locale.getCountry()); System.out.println(locale.getDisplayLanguage()+":"+locale.getLanguage()); } } }
2)ResourceBundle类,资源包
liule_en_US.properties
hello=hello world
liule_zh_CN.properties
hello=\u4F60\u597D
i18nTest2
package com.iiule.i18n; import java.util.ResourceBundle; public class i18nTest2 { public static void main(String[] args) { ResourceBundle bundle = ResourceBundle.getBundle("liule");//填的basename=properties资源包的首单词,可以定位资源包 String value = bundle.getString("hello"); System.out.println(value); } }
如果资源包名字不包括国家名与语言名,则其为默认的资源包。当在资源包里面没找到所需求的包时,通常调用默认的资源包。
3)MessageFormat类,接收对象,格式化,再转化成字符,插入合适的位置。
public static String format(String pattern(要格式化的字符),Object...arguments(可变参数,相当于数组,取代占位符))
liule_zh_CN.properties
hello=\u4F60\u597D\uFF1A{0} //0位占位符
liule_en_US.properties
hello=hello world{0}
i18nTest3.java
package com.iiule.i18n; import java.text.MessageFormat; import java.util.ResourceBundle; public class i18nTest3 { public static void main(String[] args) { ResourceBundle bundle = ResourceBundle.getBundle("liule"); String value = bundle.getString("hello"); String value1 = MessageFormat.format(value,new Object[]{"傻逼"}); System.out.println(value1); } }
4.Struts2校验执行的先后顺序:
1)首先执行校验框架(xml文件)
2)执行自定义方法的校验方法(validateMyExecute)
3)执行validate方法
例子:
login_i18n.jsp
<body>
<s:form action="LoginAction" method="post">
<s:textfield key="login.username" name="username"></s:textfield> //key为需要国际化的内容,值为prpperties的key
<s:submit key="login.submit" ></s:submit>
</s:form>
</body>
message_zh_CN.properties
login.username=\u767B\u9646
login.submit=\u63D0\u4EA4
struts.xml
<constant name="struts.custom.i18n.resources" value="message"></constant> //value值为properties文件的首个单词
拦截器的作用就是将配置文件加载到内存中,标签的作用就是将对应语言的值加载到页面中去。
例子2:(动态参数)
login_i18n.jsp
<hr> <s:text name="login.hello"> <s:param>tom</s:param> <s:param>amos</s:param> </s:text>
message_zh_CN.properties
login.hello=hello{0},I am {1}
使用<s:text>标签来调用带参数的属性值,同时使用<s:param>标签来给参数赋值,其中顺序就与参数的顺序对应。
例子3:参数值直接配置到properties属性文件中
<s:text name="login.hello"> <s:param><s:text name="login.tom"></s:text></s:param> <s:param><s:text name="login.amos"></s:text></s:param> </s:text>
login.tom=\u6C64\u59C6
login.amos=\u963F\u83AB\u65AF
注意:日期格式:中英文的日期格式不想通
补充:http://www.cnblogs.com/eggbucket/archive/2012/02/06/2339595.html
http://www.cnblogs.com/langlang/archive/2010/01/14/1647627.html