Menu

do I have to use ResourceBundle.getBundle(..)

2004-07-14
2004-07-15
  • Iain Sinclair

    Iain Sinclair - 2004-07-14

    Hi,

    Apologies in advance if I missed something obvious but my cursory search didn't shed any light...

    I have a resource called messages.properties (no points for originality there) and it's configured so that spring can use it to display my customized validation messages (this is great by the way). It also can be used with the <c:fmt > tag - all sweet so far.

    Now, I wanna access this resource in my java code.

    The question is can I access the resource bundle that I configured in my xml file via a spring method instead of using the ResourceBundle.getBundle() stuff?

    If anyone could give me a tip or point me to an example, i'd be much appreciative.

    regards
    Iain

     
    • William Jaynes

      William Jaynes - 2004-07-14

      Sorry I don't have any help to offer... But I'd like to see your configuration that let's you use <c:fmt> to write the messages. Does this require the Spring MVC stuff? Or could I use it with Struts or a plain jsp?

      Thanks, Will

       
    • Alef Arendsen

      Alef Arendsen - 2004-07-14

      The ApplicationContext interface implements MessageSource, which in its offers various getMessage() methods. If (in your bean) you implements ApplicationContextAware or extend ApplicationObjectSupport, you can reach the application context and retrieve message through it.

      As for the <c:fmt> tags (asked by jaynes in a reply to this message), if you use the Spring JstlView tag, you should be able to use the <c:fmt> tags just as you would use the <spring:message> tags.

      cheers,

      Alef

       
    • Iain Sinclair

      Iain Sinclair - 2004-07-15

      Hi Alef and Will,

      Alef, thanks for your input. You've given me something to look into. I will let you know how it goes when I get a chance - it will be a few days since I have 4 day weekend coming up ;-)

      Will, my app is largely based on the petstore example, and apart from adding the message source to the xml configuation I didn't do anything. The entry I put in  ...-servlet.xml is:

          <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
              <property name="basename"><value>WEB-INF/messages</value></property>
              <property name="defaultEncoding"><value>UTF-8</value></property>
          </bean>

      My IncludeTop.jsp file looks something like this:

      <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
      <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
      <%@ page language="java" pageEncoding="Windows-31J" contentType="text/html;charset=Windows-31J"%>

      <html><head> ...blah blah... </head>

      <c:if test="${!empty userSession.site.siteAbbr}" >
        <fmt:message key="thisSite"/> <c:out value="${userSession.site.siteAbbr}"/>
      </c:if>

      In this case the message key "thisSite" equates to a label for &#29694;&#22580;&#12288;in japanese. That's all I did and it works fine, even with Japanese text. I'm not sure how the Spring JstlView tag that Alef mentioned fits into this. As I said, I just copied stuff from petstore and it's working.

      cheers
      Iain

       

Log in to post a comment.

MongoDB Logo MongoDB