0

Working to migrate an application from Struts 1.2.x to Spring MVC, I am stuck at a place where in JSP I have to use labels from .properties lookup files.

In the legacy code, the app has used Struts' MessageTag and a custom tag using TagUtils.message() from Struts to specify the bundle name from where the value of a given key needs to fetched from.

Now with migration to Spring, I have defined all the lookup file bundles in SpringContext.xml like so:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>/WEB-INF/lookups/abc</value>
            <value>/WEB-INF/lookups/xyz</value>
            ...
        </list>
    </property>
</bean>

Using spring:message tag with the key as code worked flawlessly up until I reached a point where 2 lookup bundles use the same keys but have different value. Ideally, I would just rename the keys to be unique but that is not feasible given the code change that entails.

I am wondering if there is a way to specify the bundle name somehow so that in a case of ambiguous keys, Spring knows which bundle to fetch the values from.

I thought of extending either MessageSource or ReloadableResourceBundleMessageSource to a custom class and implement a method that provide a functionality that I am looking for but I basically hit a dead end without any documentation or example of such an implementation.

1 Answer 1

0

You're saying that abc and xyz have some overlapping keys in them? If so, one of them was overwritten by the other:

Note that message definitions in a previous resource bundle will override ones in a later bundle, due to sequential lookup.

You're going to have to use a different approach, creating multiple message source beans and loading different ones into different Spring contexts.

Sign up to request clarification or add additional context in comments.

3 Comments

I was thinking of creating multiple message source beans. Currently, the messageSource bean is defined in the SpringContext.xml. Can you provide an example of how to load beans into context based on some criteria? Do I use preHandle from HandlerInterceptor to do so before the request is processed?
I actually don't know all the various ways you could do this, but like the Spring MVC DispatcherServlet can use its own context, and you could have more than one of those if necessary, if you can separate the paths they listen on.
I ended up making the keys unique by appending the bundleName to the front of key. That avoids programmatically loading the beans into context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.