View Full Version : Accessing 'message.properties' values
Anoop
Sep 8th, 2005, 02:36 AM
Hay
How can I access the values that I had declared in my ‘message.properties’ to my business logic (ie, java code).
I can access the same in JSP using JSTL like;
<p>
<fmt:message key="user.loginName"/><br>
<fmt:message key="user.password"/>
</p>
Similarly I need the value of ‘user.loginName’ to my java code. How it is possible?
Thanks
Anoop.
:wink:
homerpsu
Jun 18th, 2007, 02:24 AM
can anyone shed more light on this? How do we access the message.props in our controllers/java beans?
sbtourist
Jun 18th, 2007, 04:30 AM
can anyone shed more light on this? How do we access the message.props in our controllers/java beans?
Hi,
here is what you have to do:
1) Declare the following bean in your Spring context:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundle MessageSource">
<property name="basename"><value>messages</value></property>
</bean>
It contains the basenames of your .properties files.
2) Make your controller (or any other bean declared in your context) implement the MessageSourceAware interface: http://www.springframework.org/docs/api/org/springframework/context/MessageSourceAware.html
3) Use the injected MessageSource object: http://www.springframework.org/docs/api/org/springframework/context/MessageSource.html
Messages will be fetched from the .properties files declared above.
Cheers,
Sergio B.
homerpsu
Jul 10th, 2007, 10:26 PM
I'm satill having a lot of trouble trying to implment this...
ApplicationContext.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundle MessageSource">
<property name="basename"><value>messages</value></property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyP laceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerD ataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
Inside my dispatch-servlet.xml :
...
<bean name="camSelectController" class="web.controller.CamSelectController">
</bean>
...
And finally my controller :
package web.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.*;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.bind.ServletRequestUtils;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.logging.*;
import web.account.*;
import web.common.*;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.MessageSource;
public class CamSelectController implements MessageSourceAware {
private MessageSource messageSource;
public void setMessageSource (MessageSource messageSource) {
this.messageSource = messageSource;
}
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
log.info (messageSource.getMessage("logo", null, Locale.CHINESE))
return new ModelAndView("camSelect");
}
}
Then I get thrown an error like this:
javax.servlet.ServletException: No adapter for handler [web.controller.CamSelectController@132d584]: Does your handler implement a supported interface like Controller?
Any code examples that other ppl have successfully used would be much appreciated! I learn best from examples!
Thanks!
scotto
Aug 2nd, 2007, 05:28 PM
Your CamSelectController needs to either implement the org.springframework.web.servlet.mvc.Controller interface or extend one of the many abstract controllers Spring provides.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.