java - How to I define and get locale-based messages in Spring MVC? -


i want define set of error messages when validation errors generate codes, codes pick corresponding error message , print them.

for sake of learning, , develop extendable web app, i'd follow proper i18n path, though need define 1 (english) set of messages now.

so locales should default english when don't find own resources (which i'm yet define).

i've never used of i18n functionality of java. , spring docs assume have knowledge.

could give me gental nudge in right direction?

i've defined messagesource in dispatcher-servlet.xml webapp context. have validator produces bindingresult object rejected field "username", code "username.taken". can generate default message.

now need error message errormessages.properties file in view.

how resolve error code message?

<bean id="messagesource"       class="org.springframework.context.support.resourcebundlemessagesource">   <property name="basenames">     <list>       <value>errormessages</value>     </list>   </property> </bean> 

it depends on want text. first possibility 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 uncommon, cause have message keys form errors object yourself.

another more common way use build in view extensions shipped spring mvc. didn't wrote guess using jsps. in case can write in jsp:

<!-- showing errors --> <div>     <form:errors path="*" /> </div>  <!-- showing arbitrary messages --> <div>     <spring:message code="${success.messagekey}"/> </div> 

for further reading suggest http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -