SpringMVC 使用validation校验无法加载properties文件,及验证信息乱码的问题

今天在使用JSR-303 validation进行校验时出现了标题中的相关问题

po类相关代码:

    @Size(min=1,max=30,message="{items.name.length.error}")
    private String name;
    @NotNull(message="{items.createtime.isNUll}")
    private Date createtime;

springmvc.xml:

<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
<!-- 校验器 -->
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
        <property name="validationMessageSource" ref="messageSource" />
    </bean>
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:CustomValidationMessages</value>
            </list>
        </property>
        <property name="fileEncodings" value="utf-8" />
        <property name="cacheSeconds" value="120" />
    </bean>
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
        <property name="validationMessageSource" ref="messageSource" />
    </bean>
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:CustomValidationMessages</value>
            </list>
        </property>
        <property name="fileEncodings" value="utf-8" />
        <property name="cacheSeconds" value="120" />
    </bean>

controller.java:

@RequestMapping("/editItemsSubmit")
    public String editItemsSubmit(Model model, HttpServletRequest request, Integer id, @Validated ItemsCustom itemsCustom, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for (ObjectError objectError : allErrors) {
                System.out.println(objectError.getDefaultMessage());
            }
            model.addAttribute("allErrors", allErrors);
            model.addAttribute("items", itemsCustom);
            return "items/editItems";
        }

CustomValidationMessage.properties:

items.name.length.error=请输入1到30个字符的商品名称
items.createtime.isNULL=请输入商品的生产日期

第一个问题很明显是properties文件中的信息未正确加载导致的。经检查发现po类中非空校验的代号为items.createtime.isNUll,properties文件中为items.createtime.isNULL,二者不匹配导致了错误。

另附springmvc加载配置properties文件的几种方式以供学习http://blog.csdn.net/chinadim/article/details/40621671

第二个问题是由于编码问题导致的

经查询将springmvc.xml文件中的

<property name="fileEncodings" value="utf-8" />

更改为:

<property name="defaultEncoding" value="utf-8" />

或者利用jdk根目录bin文件夹下的native2ascii.exe文件将properties文件转为UTF-8也可解决。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Web系统中,校验是必不可少的环节,校验一般分为前端校验和后端校验,前端校验一般使用脚本语言,对即将要提交的数据进...
    LeaveStyle阅读 8,172评论 0 0
  • SpringMVC介绍 Spring web mvc 和Struts2都属于表现层的框架,它是Spring框架的一...
    day_Sunny阅读 4,225评论 0 0
  • 对于java中的思考的方向,1必须要看前端的页面,对于前端的页面基本的逻辑,如果能理解最好,不理解也要知道几点。 ...
    神尤鲁道夫阅读 4,262评论 0 0
  • 个人学习批处理的初衷来源于实际工作;在某个迭代版本有个BS(安卓手游模拟器)大需求,从而在测试过程中就重复涉及到...
    Luckykailiu阅读 10,289评论 0 11
  • (纪念第二批天津知青赴额济纳旗插队40周年) 苗建国 列车驶离天津, 向西北疾驰, 而我的心却相反, 仍留在那片故...
    金色胡杨苗建国阅读 3,392评论 1 0

友情链接更多精彩内容