Announcement Announcement Module
Collapse
No announcement yet.
Access messages using code in java files Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access messages using code in java files

    How do I access messages and labels that are defined in resou

    I have the following defined in servlet.xml
    Code:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basenames">
    				<list>
    						<value>myapp</value>
    						<value>myapp-messages</value>
    				</list>
    		</property>
    </bean>
    I have two property files. One for labels(myapp.properties) and one for error messages (myapp-messages.properties).
    Code:
    myapp-messages.properties: 
    failedInSaveMessage=Failed while saving the customer record.

    I know how to access the messages that are defined in these property files from jsp.
    for eg:
    Code:
    <c:set var="errMessage"><spring:message code='failedInSaveMessage'/></c:set>
    <c:out value="${errMessage}"/>
    I want to access these messages from java file, so that I can prepare my errors obj after fetching
    data from DB. I am not using validator interface.

    How do I access msg text that is defined in resource files using msg code in java files?

    Basically I need something like "spring:message code='failedInSaveMessage'"

  • #2
    The following can be used off the "messageSource", if you inject it into the class you want to read the message text:

    Code:
    messageSource.getMessage(String code, Object[] args, Locale locale)
    You can pass in null for the second argument. It is for replacements in the message text. Locale can be retrieved from the HttpServletRequest.

    Comment


    • #3
      Thank you very much!

      Comment

      Working...
      X