Spring 3.0 MVC似乎忽略了messages.properties [英] spring 3.0 MVC seems to be ignoring messages.properties

查看:203
本文介绍了Spring 3.0 MVC似乎忽略了messages.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 3.0 MVC首先,我还没有找到任何关于messages.properties @ springsource 我发现关于覆盖错误消息的所有内容都在各种论坛上。如果任何人有一个参考message.properties被记录在哪里会很棒。也许messages.properties不是来自spring,而是java规范?



我试着按照关于
我的目标是用我自己的用户友好错误替换一些类型不匹配的错误消息消息

我的情况如下:

模型

  public class Test {

private int numberbomb;

public int getNumberbomb(){
return numberbomb;
}

public void setNumberbomb(int numberbomb){
this.numberbomb = numberbomb;
}
}

myservlet.xml

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

classes \messages.properties

 typeMismatch =坏值你坏坏人
test.numberbomb =你让我疯狂与这些不良输入

从表单输出


无法转换类型为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]

显示错误消息< form:errors> 以错误的方式显示自定义错误消息?我是否需要添加一些东西来弹出配置文件来告诉它查看messages.properties? Spring似乎忽略了我的messages.properties文件(它位于WEB-INF \classes文件夹中)

感谢您的任何想法!

我的一位同事指出我的方向正确。我更改了myservlet.xml中的messageSource bean

; 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>

无论什么原因解决了这个问题。谢谢同事! :)

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

public class Test {

    private int numberbomb;

    public int getNumberbomb() {
        return numberbomb;
    }

    public void setNumberbomb(int numberbomb) {
        this.numberbomb = numberbomb;
    }
}

myservlet.xml

<mvc:annotation-driven/>

jsp

<form:form id="test" method="post" modelAttribute="test">

<form:errors path="*"/>

<form:label path="numberbomb">numberbomb</form:label>
<form:input path="numberbomb"/>

</form:form>

classes\messages.properties

typeMismatch=bad value you bad bad person
test.numberbomb=you are driving me crazy with these bad inputs

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 <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)

Thanks for any ideas!

解决方案

An associate of mine pointed me in the right direction. I changed the messageSource bean in myservlet.xml

from

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="messages" />
    <property name="cacheSeconds" value="1" />
</bean>

to

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

For whatever reason this solved the problem. Thanks associate! :)

这篇关于Spring 3.0 MVC似乎忽略了messages.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆