I want to define a set of error messages so that when validation errors generate codes, those codes pick up the corresponding error message and print them.
For the sake of learning, and to develop an extendable web app, I'd like to follow the proper i18n path, though I only need to define one (english) set of messages now.
So the locales should all default to english when they don't find their own resources (which I'm yet to define).
I've never used any of the i18n functionality of Java. And the spring docs assume that I have this knowledge.
Could someone just give me a gental nudge in the right direction?
I've defined a messageSource
in my dispatcher-servlet.xml
webapp context. I have a validator which produces a BindingResult
object with a rejected field "username"
, with code "username.taken"
. I can generate the default message.
Now I just need to get the error message from the errormessages.properties
file in my view.
How do I resolve an error code to a message?
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>errormessages</value>
</list>
</property>
</bean>
Access it via http://localhost:8080/SpringMVC/welcome.htm, change the locale by clicking on the language’s link. 1. English locale – http://localhost:8080/SpringMVC/welcome.htm?language=en 2. Chinese locale – http://localhost:8080/SpringMVC/welcome.htm?language=zh_CN Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter.
To make Spring MVC application supports the internationalization, register two beans : 1. SessionLocaleResolver Register a “SessionLocaleResolver” bean, named it exactly the same characters “localeResolver“. It resolves the locales by getting the predefined attribute from user’s session.
Most of the web application frameworks provide easy ways to localize the application based on user locale settings. Spring also follows the pattern and provides extensive support for internationalization (i18n) through the use of Spring interceptors, Locale Resolvers and Resource Bundles for different locales.
This is happening because of our SessionLocaleResolver which keeps the current locale throughout the session if we don’t pass any locale explicitly. How to load the property from messages property file in the java file ?
It depends on what you want to do with this text. The first possibility is to get the message programmatically:
@Autowired
private MessageSource messageSource;
@RenderMapping(params = "render=details")
public String showDetails (Model model, Locale locale) {
messageSource.getMessage(<your key goes here>, null, locale);
}
This way is very uncommon, cause you have to get the message keys form the Errors object by yourself.
Another more common way is to use the build in view extensions shipped with spring mvc. You didn't wrote it but I guess your using JSPs. In that case you can simply write something like this in your JSP:
<!-- showing errors -->
<div>
<form:errors path="*" />
</div>
<!-- showing arbitrary messages -->
<div>
<spring:message code="${success.messageKey}"/>
</div>
For further reading I suggest you http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With