想要使用UNICODE校验汉字,未成功。在validate中指定UNICODE(例如/u0030)时,并不像想像中可以检测出UNICODE对应的字符。
validate.xml:
<field property="did" depends="required,mask">
<msg name="required" key="menuForm.test.required" resource="true"/>
<msg name="mask" key="menuForm.test.mask" resource="false"/>
<var>
<var-name>mask</var-name>
<var-value>^[/u0030]*$</var-value>
</var>
</field>
运行效果:
u、0、3均可以通过校验。
原因:
org.apache.struts.validator.FieldChecks.FieldChecks的validateMask()在读取validate.xml中的正则表达式的时候,将UNICODE读取成了String。因此,在调用 org.apache.commons.validator.GenericValidator的matchRegexp()方法进行校验时,u、0、3均可以通过校验。 奇怪的是“/”或是“//”都无法通过校验。

public static boolean matchRegexp(String value, String regexp) ...{
if (regexp == null || regexp.length() <= 0) ...{
return false;
}
Perl5Util matcher = new Perl5Util();
System.out.println("Before match regexp:[" + regexp + "]");
System.out.println("Before match value:[" + value + "]");
System.out.println(matcher.match("/" + regexp + "/", value));
return matcher.match("/" + regexp + "/", value);
}
运行结果:
Before match regexp:[^[/u0030]*$]
Before match value:[u]
true

public static void main(String[] args) ...{
String value = "u";
String regexp = "^[0]*$";
Perl5Util matcher = new Perl5Util();
System.out.println("Before match regexp:[" + regexp + "]");
System.out.println("Before match value:[" + value + "]");
System.out.println(matcher.match("/" + regexp + "/", value));
}运行结果:
Before match regexp:[^[0]*$]
Before match value:[u]
true
网上还有人说UNICODE只在*.properties 文件中好用,而在 *.XML文件中不好用(including UNICODE
codes was only possible in *.properties files, not in *.XML)。 ??
还有,You have to declare unicode characters using character entities in xml.
结论,不能使用validate.xml来进行UNICODE的校验。
尝试使用Unicode进行汉字校验未成功。在Struts的validate.xml中指定Unicode时,无法正确识别并匹配特定字符。问题在于正则表达式被当作字符串处理,导致匹配失败。
5371

被折叠的 条评论
为什么被折叠?



