开发者

Spring multiple error messages

开发者 https://www.devze.com 2023-03-09 15:16 出处:网络
I suppose that everybody that uses spring, uses form binding and validation. And you all defined the messages to display on validation errors. I did it with this in my config:

I suppose that everybody that uses spring, uses form binding and validation. And you all defined the messages to display on validation errors. I did it with this in my config:

<bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMes开发者_Python百科sageSource"
        p:basename="messages" />

what will happen basically is that it will read messages.properties in my root folder of the project.

But I'd need to put messages in two separate files. Because one part of the app has to be standalone. I tried adding this just after the one above:

<bean id="messageSourceAssistenza"
        class="org.springframework.context.support.ResourceBundleMessageSource"
        p:basename = "com.mypackage.other.assistenzamessages.properties"
        />

but it can't resolve those messages at all. How to solve this?


You should be able to use ResourceBundleMessageSource.setBasenames that accepts array of base names:

Set an array of basenames, each following ResourceBundle conventions: essentially, a fully-qualified classpath location. If it doesn't contain a package qualifier (such as org.mypackage), it will be resolved from the classpath root.

The associated resource bundles will be checked sequentially when resolving a message code. Note that message definitions in a previous resource bundle will override ones in a later bundle, due to the sequential lookup.

Sample configuration as follows:

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>messages_1</value>
            <value>messages_2</value>
            ...
            <value>messages_n</value>
        </list>
    </property>
</bean>
0

精彩评论

暂无评论...
验证码 换一张
取 消