Friday, June 27, 2008
Spring + ResourceBundleMessageSource + Arguments
Recently I had a request to move some hard-coded error messages from the source files to an external properties file. At first I ran into some issues since we were using parameters, but spring's taglib allows for that so here goes:
Spring config
com/harryfwong/shared/messages.properties
displayError.jsp
Spring config
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com.harryfwong.shared.messages">
</property>
com/harryfwong/shared/messages.properties
error.testingParameters=Are you passing in Parameters: {0}, {1}?
displayError.jsp
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<spring:message code="error.testParameters" argumentseparator=";" arguments="param1;param2">
</spring:message>
Labels: arguments, Java, JSP, Messages, ResourceBundleMessageSource, Spring
Comments:
<< Home
Great one nice and simple..just a minor thing, property name in "messages.properties" should be consistent with the one specified in the JSP..either rename "testParameters" to "testingParameters" or vice versa
Cheers
Post a Comment
Cheers
<< Home