Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resource Bundle Spring

How can I access the messages from a resource bundle in Spring MVC inside a class that extends from AbstractController?

I have tried getMessageSourceAccessor().getMessage("a.message"); but it it throws this Exception:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.context.NoSuchMessageException: No message found under code 'a.message' for locale 'en'.

When I do the same inside a jsp it works

<spring:message code="a.message"/>
like image 228
Enrique Avatar asked Nov 08 '09 01:11

Enrique


People also ask

What is ResourceBundle in spring?

Let's create a simple Spring Boot application from start.spring.io. The first step is to create a resource bundle (a set of properties files with the same base name and language suffix) in the resources package. I will create properties files with base name texts and only one key greeting: texts_en. properties.

What is the use of ResourceBundle?

Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user's locale.

What is bundle resource?

A BUNDLE resource defines a CICS® bundle, a unit of deployment for an application. A bundle is a collection of CICS resources, artifacts, references, and a manifest that you can deploy into a CICS region to represent a whole application or a component of an application.

What is the ResourceBundle class?

ResourceBundle class is used to store text and objects which are locale sensitive. Generally we use property files to store locale specific text and then represent them using ResourceBundle object. Following are the steps to use locale specific properties file in a java based application.


1 Answers

You can use one of the getMessage() method variants on the org.springframework.web.servlet.support.RequestContext class. Something like this works:

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    RequestContext ctx = new RequestContext(request);
    String messageFromBundle = ctx.getMessage("a.message");
}
like image 78
khill Avatar answered Oct 29 '22 05:10

khill