2012-07-24 49 views
4

我昨天工作過,然後做了一些工作,現在我一直試圖修復它幾個小時,但我無法再繼續工作了。驗證郵件沒有從郵件屬性文件中獲取到Spring中

我有一個包含<form:form>的Spring MVC應用程序,當用戶輸入錯誤信息時,我想從.properties文件中顯示自定義錯誤消息(<form:errors>)。 JSR-303註釋中定義了什麼「錯誤」。從表單

摘錄:

<form:form method="post" action="adduserprofile" modelAttribute="bindableUserProfile"> 
<table> 
    <tr> 
     <td><form:label path="firstName">Voornaam</form:label></td> 
     <td> 
      <form:input path="firstName"/> 
      <form:errors path="firstName" /> 
     </td> 
    </tr> 
    <tr> 
     <td><form:label path="lastName">Achternaam</form:label></td> 
     <td> 
      <form:input path="lastName"/> 
      <form:errors path="lastName" /> 
     </td> 
    </tr> 

從BindableUserProfile摘錄:從控制器

@NotNull 
@Size(min = 3, max = 40, message="{errors.requiredfield}") 
public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

@NotNull 
@Size(min = 3, max = 40, message="errors.requiredfield") 
public String getLastName() { 
    return lastName; 
} 

摘錄:

@RequestMapping(value = "/edit/{userProfileId}", method = RequestMethod.GET) 
public String createOrUpdate(@PathVariable Long userProfileId, Model model) { 
    if (model.containsAttribute("bindableUserProfile")) { 
     model.addAttribute("userProfile", model.asMap().get("bindableUserProfile")); 
    } else { 
     UserProfile profile = userProfileService.findById(userProfileId); 
     if (profile != null) { 
      model.addAttribute(new BindableUserProfile(profile)); 
     } else { 
      model.addAttribute(new BindableUserProfile()); 
     } 
    } 

    model.addAttribute("includeFile", "forms/userprofileform.jsp"); 
    return "main"; 
} 

@RequestMapping(value = "/adduserprofile", method = RequestMethod.POST) 
public String addUserProfile(@Valid BindableUserProfile userProfile, BindingResult result, Model model) { 
    if (result.hasErrors()) { 
     return createOrUpdate(null, model); 
    } 

    UserProfile profile = userProfile.asUserProfile(); 
    userProfileService.addUserProfile(profile); 
    return "redirect:/userprofile"; 
} 

從應用程序的context.xml摘錄

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

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> 
    <property name="validationMessageSource"> 
     <ref bean="messageSource"/> 
    </property> 
</bean> 

在資源/消息中我有兩個文件,messages_en.properties和messages_nl.properties。兩者具有相同的,簡單的內容:

errors.requiredfield=This field is required!!! 
  • 當我提交表單用空的名字我可以在控制器方法「addUserProfile()」的錯誤確實發現看到的。
  • 當我用空的名字提交表單時,在字段旁邊顯示了消息標識符,即在姓氏的情況下字面文本「errors.requiredfield」或「{errors.requiredfield}」。
  • 當我將消息屬性值更改爲「Foo」而不是「Foo」時顯示爲錯誤消息。所以錯誤機制本身似乎很好。
  • application-context.xml中的messageSource bean必須是正確的,因爲它表示在更改基本名稱時找不到屬性文件。
  • 空輸入未被NotNull註釋捕獲。 Spring將空輸入視爲空字符串,而不是空。

所以,看起來屬性文件被找到並且驗證註解被正確處理,但是Spring並不理解它必須用屬性文件中的消息替換消息鍵。

回答

2

Yaaaargh,我認爲這是從來沒有工作的第一個地方。

我認爲有可能將JSR-303註釋的「message」屬性解釋爲一個鍵值,以便從message.properties文件中獲取關聯的錯誤消息,但是我認爲我錯了。

@Size(min = 3, max = 40, message="errors.requiredfield")

我在工作中collegue編程創建這種行爲對我們是一個層,但默認情況下不工作。它好像我有它的工作一次,因爲我用

@Size(min = 3, max = 40, message="{errors.requiredfield}")

大括號引起春天開始查找和替換使用的程序。屬性文件作爲源。這第二個選項仍然工作。

+0

你能從.properties文件中設置min和max嗎? – luksmir 2013-10-31 12:18:43

2

我從過去的1.5天一直在做同樣的事情,最後我找到了解決方案。

可能聽起來有點瘋狂,但它的工作解決方案。 :)

@Size(min = 1, max = 50, message = "Email size should be between 1 and 50") 

現在從驗證標記中刪除message = "Email size should be between 1 and 50"

完成此操作後,您的註釋將會是這樣。

@Size(min = 1, max = 50) 

現在在控制器端調試提交表單時被調用的方法。以下是我用戶點擊提交時接收請求的方法。

public static ModelAndView processCustomerLoginRequest(IUserService userService, LoginForm loginForm, 
     HttpServletRequest request, HttpSession session, BindingResult result, String viewType, Map<String, LoginForm> model) 

現在在方法的第一行放置一個調試點並調試參數「result」。

BindingResult result 

在翻譯時,您會在代碼數組中找到這樣的字符串。

Size.loginForm.loginId 

現在在你的屬性文件中定義這個字符串和一個針對該字符串的消息。 編譯並執行。該消息將在該註釋不被驗證時顯示。

Size.loginForm.loginId=email shouldn't be empty. 

基本上春天讓自己的字符串作爲關鍵,它的屬性文件的消息。在上述關鍵:

  • Size(@Size) =驗證註釋名稱
  • loginForm =我的類名
  • loginId =屬性名loginForm類。

這種方法的美妙之處在於,它將在您使用Spring國際化時運行良好。它會根據語言變化自動切換消息文件。