View Full Version : Accessing ResourceBundle using annotations in controller?
Goll97
Feb 16th, 2009, 07:17 PM
Hey guys, I am hoping this is a simple question.
I'm trying to access my message.properties file, but i don't know how to do it when using annotations in my controller?
Thanks.
shahinscc
Feb 16th, 2009, 10:13 PM
Let, there is a file named shared.properties. It has a property named 'login.app.url'. We will read it using annotated controller.
We have to read it in a static block in Annotated controller.
This is my annotated Controller.
@SuppressWarnings("unchecked")
@Controller
@RequestMapping("/myView")
public class MyController {
private final Logger log = Logger.getLogger(getClass());
private static String LOGIN_APP_URL;
static {
InputStream is = MyController.class.getResourceAsStream("/shared.properties");
if (is == null) {
throw new RuntimeException("shared.properties not found in classpath");
}
Properties props = new Properties();
try {
props.load(is);
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
LOGIN_APP_URL = props.getProperty("login.app.url");
if (LOGIN_APP_URL == null) {
throw new RuntimeException("Property 'login.app.url' not found in shared.properties");
}
}
}
Goll97
Feb 16th, 2009, 10:19 PM
thanks for the reply, I found a way of doing it (I might use what you did in different scenario)
1) I auto wire the application context
@Autowired
private ApplicationContext context;
2) in any method that needs to pull a msg i do the following
context().getMessage("key", null, Locale.getDefault());
shahinscc
Feb 16th, 2009, 10:30 PM
Thanks a lot for sharing the way you found.
It will help me in some cases.
ramesh.raj
Apr 19th, 2009, 03:13 AM
I tried i18n support using SWF 2 and is ok for views when done as told in the ref guide.
But now I want the i18n support of SWF to be used in my controllers and/or model classes also.
In my flows I have a file named messages.properties and the code and value of the messages declared here can be used in views.
Now how to access the same code and value of the messages defined here in the controller classes? How can I achieve this, some examples expected.
Thanks.
zhangxin
Jun 9th, 2009, 10:24 PM
how to implement integrate with strusts?
Thanks
ramesh.raj
Jun 9th, 2009, 10:30 PM
can you elaborate more the problem you encountered?
zhangxin
Jun 9th, 2009, 10:34 PM
Hi Ramesh,raj,
Thanks for your prompt response.
Currently, I am integrate struts and spring. I want to access resource bundling (e.g. message.properties) values in my action class. Would I know how to do it?
Many thanks. :)
zhangxin
Jun 9th, 2009, 10:52 PM
Hi Ramesh.raj,
I find how to do it based on previous threads.
Just share what I did
in my spring-struts-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
Note, it is import which mark in red. as it not work if you miss it.
<bean id="messageSource" class="org.springframework.context.support.ReloadableReso urceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages"/>
<property name="cacheSeconds" value="10"/>
</bean>
In the action
private ApplicationContext context;
@Autowired
public void setContext(ApplicationContext context) {
this.context = context;
}
String message=context.getMessage("successful", null, Locale.getDefault()));
zhangxin
Jun 12th, 2009, 01:32 AM
Just share if you are using controller
then code as below:
getMessageSourceAccessor().getMessage("key");
Ponzy
Jun 16th, 2009, 03:24 AM
thanks for the reply, I found a way of doing it (I might use what you did in different scenario)
1) I auto wire the application context
@Autowired
private ApplicationContext context;
2) in any method that needs to pull a msg i do the following
context().getMessage("key", null, Locale.getDefault());
I had a similar problem.
I tried your solotion and it works perfectly.
Thank you very much!
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.