簡體   English   中英

Spring MVC表單驗證消息未加載

[英]Spring MVC form validation messages not loading

我正在嘗試在資源文件中定義錯誤消息,但似乎無法加載它們,因此可以在控制器中使用它們。

我的servlet-context.xml文件:

<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <beans:property name="basename" value="classpath:messages" />
</beans:bean>

我的表格

public class LoginForm {
    @NotEmpty
    private String username;
    // getters and setters
}

我的messages.properties文件(在我的源文件夾的根目錄中,因此它被編譯到我的classes文件夾的根目錄中):

NotEmpty.loginForm.username=test1
NotEmpty.username=test2
NotEmpty.java.lang.String=test3
NotEmpty=test4

我的控制器

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@Valid LoginForm form, BindingResult result) {
    if (result.getFieldErrors().size() > 0) {
        FieldError firstError = result.getFieldErrors().get(0);
        System.out.println(firstError.getDefaultMessage());
    }
    // ...
}

但是, getDefaultMessage調用的輸出永遠不會是我在資源文件中定義的輸出! 它始終may not be empty (默認值)。

我在上下文文件中嘗試了各種不同的條目,但似乎沒有加載資源文件。 我究竟做錯了什么?

根據此文檔 ,您需要做的是在項目類路徑的根目錄下放置一個名為ValidationMessages.properties的屬性文件。

然后,您可以使用以下格式添加屬性

org.hibernate.validator.constraints.NotEmpty.message=Default message, can not be empty 
NotEmpty.ClassName.fieldName=fieldName can not be empty.

其中ClassName是您的類的名稱, fieldName是該類的字段的名稱。

您可能需要進行一些配置以設置正確的MessageInterpolator ,但是我認為默認值將滿足您的需求。

第一個想法:

嘗試將屬性鍵從NotEmpty.loginForm.username重命名為NotEmpty(僅檢查messageSource是否正常工作)。 如果仍然無法正常工作

第二個主意:

您在哪里執行組件掃描? 假設您有兩個spring配置:applicationContext.xml(或其他名稱)和servlet-context.xml。

如果在applicationContext中具有這樣的結構<context:component-scan base-package="by.company.app" /> ,則您的控制器沒有定義的messageSource-因此它無法加載自定義消息。 這是因為@Controller bean在一個Spring上下文中,而MessageSource bean在另一個上下文中。 (要進行檢查,您可以在控制器中簡單聲明@Autowired MessageSource messageSource字段,並在調試中查看它是否為null)在這種情況下,您可以將applicationContext中的component-scan修改為:

<context:component-scan base-package="by.company.app">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

並將以下配置添加到servlet-context.xml:

<context:component-scan base-package="by" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

希望這可以幫助

不幸的是,看起來必須定義一個MessageSource,然后在適當的Java類中使用它。 getDefaultMessage()方法似乎無法從消息屬性讀取。

在組件掃描的類中:

@Autowired
private MessageSource messageSource;
public String getMessage(FieldError field) {
    return messageSource.getMessage(field, null);
}

這將遍歷消息屬性文件中的所有可能性,然后(如果未找到任何內容)返回到getDefaultMessage()結果。

另外,我更新了servlet-context.xml文件以使用/WEB-INF/classes/messages messages的基本名稱值定義我的messageSource,這與上面的問題相反。

我花了一些時間解決此問題,做了以下更改,現在在Spring 4.0 MVC和Hibernate 4.1.9中可以正常工作

1.將消息放在源文件夾下。

  1. 如果ReloadableResourceBundleMessageSource無法正常工作,請更改為

    類= “org.springframework.context.support.ResourceBundleMessageSource”>

  2. 更改屬性,例如'<'property name =“ basename” value =“ messages” />

  3. 在Message.properties文件中,密鑰應采用以下格式。

    NotEmpty.userForm.userId

     userForm-->Model class name. 

(如果您的Model類名稱為UserForm,則應該
被稱為userForm)

    userId--> attribute of userForm class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM