2017-02-17 29 views
0

嘗試在屬性中包含所有錯誤消息。Spring 4 MVC JSR303 @有限屬性錯誤消息

以下教程:https://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/

問題:錯誤消息不是從我的屬性文件來了。不知道我做錯了

文件結構:

enter image description here

POJO

import javax.validation.constraints.DecimalMin; 
import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Pattern; 
import javax.validation.constraints.Size; 

@Entity 
@Table(name="books") 
public class Book implements Serializable{ 

private static final long serialVersionUID = -2042607611480064259L; 

@Id 
@GeneratedValue 
private int id; 

@NotNull 
@Size(min=7) 
private String name; 

@NotNull 
@Size(min=2, max=13) 
private String ispn; 

@DecimalMin(value = "0.01") 
private double price; 

public Book(){} 


// Setter & getters 

} 

exception_en_US.properties

NotNull.book.name = Book name must not be blank, Please insert valid book name. 
Size.book.name = Book name should have more than 7 characters. 

NotNull.book.ispn = Must enter valid ISPN code. 
Size.book.ispn = Standard ISPN code should have 10, 13 characters. 

DecimalMin.book.price = Price of the book must be greater than 0. And can not be Negative number. 

應用,調度員servlet.xml中

<context:component-scan base-package="com.app.controller" /> 

<mvc:annotation-driven/> 

<bean id="localeResolver" 
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> 
    <property name="defaultLocale" value="en" /> 
</bean> 

<mvc:interceptors> 
    <bean id="localeChangeInterceptor" 
     class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
     <property name="paramName" value="language" /> 
    </bean> 
</mvc:interceptors> 

<!-- Binding properties to context --> 

<bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basenames"> 
     <list> 
      <value>com.app.properties.windows</value> 
      <value>com.app.properties.exceptions</value> 
     </list> 
    </property> 

</bean> 

我在做什麼錯?你的額外的眼睛非常感謝。

謝謝

+0

我可以將它發送到我的郵箱嗎?謝謝 – lpgad

+0

因爲我的eclipse插件總是沒有安裝 – lpgad

回答

0

事實證明exception_en_US.properties不應引用類和領域,而是形成&路徑名,表單名稱,這是命令名或給出的ModelAttribute。

這是我的HTML

<sf:form action="/newbook" modelAttribute="formBook" method="POST"> 
     <table> 
     <tr> 
      <td>Book Name:</td> 
      <td> 
       <sf:input type='text' name='name' path="name"/><br/> 
       <sf:errors path="name"></sf:errors> 
      </td> 
     </tr> 
     <tr> 
      <td>ispn:</td> 
      <td> 
       <sf:input type='text' name='ispn' path="ispn"/><br/> 
       <sf:errors path="ispn"></sf:errors> 
      </td> 
     </tr> 
     <tr> 
      <td>Price:</td> 
      <td> 
       <sf:input type='text' name='price' path="price"/><br/> 
       <sf:errors path="price"></sf:errors> 

      </td> 
     </tr> 
     <tr><td colspan='2'><input name="submit" type="submit" value="submit"/></td></tr> 

     </table> 
    </sf:form> 

注意:modelAttribute="formBook"

所以在我exception_en_US.properties

NotNull.formBook.name = Book name must not be blank, Please insert valid book name. 
Size.formBook.name = Book name should have more than 7 characters. 

NotNull.formBook.ispn = Must enter valid ISPN code. 
Size.formBook.ispn = Standard ISPN code should have 10, 13 characters. 

希望這有助於其他人......我花了洙的sooo長...

1

我認爲你的messageSource配置是錯誤的。嘗試是這樣的:

<bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basenames"> 
     <list> 
      <value>classpath:window</value> 
      <value>classpath:exception</value> 
     </list> 
    </property> 
</bean> 
+0

我以爲這個值需要被限制名稱......編輯:我試過了,這沒有奏效。當我做這個window.properties沒有加載.....你需要我拍攝我的文件夾的屏幕截圖?會有幫助嗎? –

+0

好的,請這樣做。 – mhshimul

+0

非常感謝您花時間上傳文件結構......所以我的代碼沒有什麼看起來錯了? –