gpt4 book ai didi

java - Spring 4 MVC JSR303 @Valid 属性错误消息

转载 作者:行者123 更新时间:2023-12-01 08:52:28 24 4
gpt4 key购买 nike

尝试将所有错误消息都包含在属性中。

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

问题:错误消息不是来 self 的属性文件。不知道我做错了什么

文件结构:

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>

我做错了什么?非常感谢您多加关注。

谢谢

最佳答案

我认为你的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>

关于java - Spring 4 MVC JSR303 @Valid 属性错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42299140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com