I found another solution which allows the conversion to happen in the enum, as I use them from a number of controllers, and have a lot of enums too so the converter method isn't practical.
I defined a bean in webmvc-config.xml
To inject the application context into this class which sets it in AppContextCode:<bean id="contextApplicationContextProvider" class="ApplicationContextProvider"></bean>
Code:public class ApplicationContextProvider implements ApplicationContextAware { public void setApplicationContext(ApplicationContext ctx) throws BeansException { // Wiring the ApplicationContext into a static method AppContext.setApplicationContext(ctx); } }Then in the enum:Code:public class AppContext { private static ApplicationContext ctx; /** * Injected from the class "ApplicationContextProvider" which is automatically * loaded during Spring-Initialization. */ public static void setApplicationContext(ApplicationContext applicationContext) { ctx = applicationContext; } /** * Get access to the Spring ApplicationContext from everywhere in your Application * * @return */ public static ApplicationContext getApplicationContext() { return ctx; } }
Code:public enum MyEnum { VALUEONE("valueOne.text"), VALUETWO("valueTwo.text"); MyEnum(String s) { label = s; } String label; public String getLabel() { Locale locale = LocaleContextHolder.getLocale(); return AppContext.getApplicationContext().getMessage(label, null, locale); } public String toString() { return getLabel(); } }


Reply With Quote