在Struts 动态校验框架中校验UNICODE的问题

来源:互联网 发布:多益网络面试通过率 编辑:程序博客网 时间:2024/04/30 22:58
 

想要使用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的校验。

原创粉丝点击