SpringMVC 使用validation校驗無法加載properties文件,及驗證信息亂碼的問題

今天在使用JSR-303 validation進行校驗時出現(xiàn)了標(biāo)題中的相關(guān)問題

po類相關(guān)代碼:

    @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=請輸入商品的生產(chǎn)日期

第一個問題很明顯是properties文件中的信息未正確加載導(dǎo)致的。經(jīng)檢查發(fā)現(xiàn)po類中非空校驗的代號為items.createtime.isNUll,properties文件中為items.createtime.isNULL,二者不匹配導(dǎo)致了錯誤。

另附springmvc加載配置properties文件的幾種方式以供學(xué)習(xí)http://blog.csdn.net/chinadim/article/details/40621671

第二個問題是由于編碼問題導(dǎo)致的

經(jīng)查詢將springmvc.xml文件中的

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

更改為:

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

或者利用jdk根目錄bin文件夾下的native2ascii.exe文件將properties文件轉(zhuǎn)為UTF-8也可解決。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Web系統(tǒng)中,校驗是必不可少的環(huán)節(jié),校驗一般分為前端校驗和后端校驗,前端校驗一般使用腳本語言,對即將要提交的數(shù)據(jù)進...
    LeaveStyle閱讀 3,119評論 0 0
  • SpringMVC介紹 Spring web mvc 和Struts2都屬于表現(xiàn)層的框架,它是Spring框架的一...
    day_Sunny閱讀 894評論 0 0
  • 對于java中的思考的方向,1必須要看前端的頁面,對于前端的頁面基本的邏輯,如果能理解最好,不理解也要知道幾點。 ...
    神尤魯?shù)婪?/span>閱讀 897評論 0 0
  • 個人學(xué)習(xí)批處理的初衷來源于實際工作;在某個迭代版本有個BS(安卓手游模擬器)大需求,從而在測試過程中就重復(fù)涉及到...
    Luckykailiu閱讀 4,975評論 0 11
  • (紀(jì)念第二批天津知青赴額濟納旗插隊40周年) 苗建國 列車駛離天津, 向西北疾馳, 而我的心卻相反, 仍留在那片故...
    金色胡楊苗建國閱讀 526評論 1 0

友情鏈接更多精彩內(nèi)容