簡體   English   中英

Spring 4 MVC JSR303 @Valid錯誤消息到屬性

[英]Spring 4 MVC JSR303 @Valid error message to property

試圖將我所有的錯誤消息都放在一個屬性中。

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

問題:錯誤消息不是來自我的屬性文件。 不知道我在做什么錯

檔案結構:

在此處輸入圖片說明

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_zh_CN.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.

app-dispatcher-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>

我究竟做錯了什么? 非常感謝您的關注。

謝謝

我認為您的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>

事實證明, exception_en_US.properties不應引用類和字段,而應引用由commandName或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.

希望這可以幫助其他人...讓我這么久...

暫無
暫無

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

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