Spring 3.0 MVC似乎忽略了messages.properties [英] spring 3.0 MVC seems to be ignoring messages.properties
问题描述
我试着按照关于 我的情况如下: 模型 myservlet.xml classes \messages.properties 从表单输出 无法转换类型为java.lang的属性值。字符串到所需的类型int为属性numberbomb;嵌套异常是org.springframework.core.convert.ConversionFailedException:无法将java.lang.String类型的值three转换为int类型;嵌套异常是java.lang.NumberFormatException:对于输入字符串:three 控制器中的BindingResult.toString() 字段'numberbomb'的对象'test'中的字段错误:被拒绝的值[three];代码[typeMismatch.test.numberbomb,typeMismatch.numberbomb,typeMismatch.int,typeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:codes [test.numberbomb,numberbomb];参数[];默认消息[numberbomb]];默认消息[无法将'java.lang.String'类型的属性值转换为属性'numberbomb'所需的类型'int';嵌套异常是org.springframework.core.convert.ConversionFailedException:无法将类型'java.lang.String'中的值three转换为类型'int';嵌套的异常是java.lang.NumberFormatException:对于输入字符串:three] 显示错误消息 感谢您的任何想法! 我的一位同事指出我的方向正确。我更改了myservlet.xml中的messageSource bean 至 无论什么原因解决了这个问题。谢谢同事! :) Spring 3.0 MVC First of all, I haven't found any documentation regarding messages.properties @ springsource Everything I've found about overriding error messages has been on various forums. If anyone has a reference to where messages.properties is documented that'd be fantastic. Maybe messages.properties comes not from spring but a java spec? I have tried following the advice on
JSR-303 Type Checking Before Binding My goal is to replace some type mismatch error messages with my own user friendly error messages My situation is as follows: Model myservlet.xml jsp classes\messages.properties Output from the form Failed to convert property value of type java.lang.String to required type int for property numberbomb; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "three" from type java.lang.String to type int; nested exception is java.lang.NumberFormatException: For input string: "three" BindingResult.toString() within my controller Field error in object 'test' on field 'numberbomb': rejected value [three]; codes [typeMismatch.test.numberbomb,typeMismatch.numberbomb,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [test.numberbomb,numberbomb]; arguments []; default message [numberbomb]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'numberbomb'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "three" from type 'java.lang.String' to type 'int'; nested exception is java.lang.NumberFormatException: For input string: "three"] Is displaying the error messages with Thanks for any ideas! An associate of mine pointed me in the right direction. I changed the messageSource bean in myservlet.xml from to For whatever reason this solved the problem. Thanks associate! :) 这篇关于Spring 3.0 MVC似乎忽略了messages.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
我的目标是用我自己的用户友好错误替换一些类型不匹配的错误消息消息
public class Test {
private int numberbomb;
public int getNumberbomb(){
return numberbomb;
}
public void setNumberbomb(int numberbomb){
this.numberbomb = numberbomb;
}
}
< mvc:annotation-driven />
< form:form id =testmethod =postmodelAttribute =test>
< form:errors path =*/>
< form:label path =numberbomb> numberbomb< / form:label>
< form:input path =numberbomb/>
< / form:form>
typeMismatch =坏值你坏坏人
test.numberbomb =你让我疯狂与这些不良输入
< form:errors>
以错误的方式显示自定义错误消息?我是否需要添加一些东西来弹出配置文件来告诉它查看messages.properties? Spring似乎忽略了我的messages.properties文件(它位于WEB-INF \classes文件夹中)
; bean id =messageSourceclass =org.springframework.context.support.ReloadableResourceBundleMessageSource>
< property name =basenamevalue =messages/>
< property name =cacheSecondsvalue =1/>
< / bean>
< bean id =messageSourceclass =org.springframework.context.support.ResourceBundleMessageSource>
< property name =basenamevalue =messages/>
< / bean>
public class Test {
private int numberbomb;
public int getNumberbomb() {
return numberbomb;
}
public void setNumberbomb(int numberbomb) {
this.numberbomb = numberbomb;
}
}
<mvc:annotation-driven/>
<form:form id="test" method="post" modelAttribute="test">
<form:errors path="*"/>
<form:label path="numberbomb">numberbomb</form:label>
<form:input path="numberbomb"/>
</form:form>
typeMismatch=bad value you bad bad person
test.numberbomb=you are driving me crazy with these bad inputs
<form:errors>
the wrong way display custom error messages? Do I need to add something to spring config files to tell it to look at messages.properties? Spring seems to be ignoring my messages.properties file (which is located in the WEB-INF\classes folder)<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="cacheSeconds" value="1" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>