Saturday, June 18, 2011

How do I use my web resource bundle within my Spring controller?



Here is how I did it:



In my controller:




import org.springframework.context.ApplicationContext; ...
@Autowired private ApplicationContext context;



In the resource bundle ApplicationResources.properties


gotocode.error.notfound=Sorry {0} is not recognized as a code. 



Back in the controller




public static final String GOTOCODE_NOTFOUND_KEY = "gotocode.error.notfound";
public
static final String CODE_NOT_FOUND_MESSAGE = "Sorry the entry {0} is not recognized as a code.";
...
public ModelAndView findNextInSeries(String code,..
String errorMessage = context.getMessage(
GOTOCODE_NOTFOUND_KEY, new Object[] { code }, CODE_NOT_FOUND_MESSAGE, Locale.getDefault());   

Previously my ApplicationResources.properties was configured in the web.xml







http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_1270163143862"></web-app>

<display-name>Sample Spring Resource Bundle
<distributable>

<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext
<param-value>ApplicationResources



As well as in the Spring Application context configuration (dispatcher-servlet.xml):

No comments:

About Me

My photo
Lead Java Developer Husband and Father

Tags