Announcement Announcement Module
No announcement yet.
Get ApplicationContext in JSP? ResourceContext? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get ApplicationContext in JSP? ResourceContext?


    I have an EAR which contains 3 war's and a Common library.
    In my Common library I have a TagHandler which is to be reused between the 3 wars. Part of what the TagHandler needs to do is look up a war-specific property file. I have a messageSource set up for each ApplicationContext.

    What I need now is to get a reference to the particular ApplicationContext that any JSP is running in and pass it to the TagHandler so that it can do a property look up on the correct MessageSource.

    I have seen ResourceContext which I believe can be used within a JSP but not quite sure how to use it.

    Anyone any ideas for getting the ApplicationContext in a JSP?


  • #2
    You can define a tag library that extends RequestContextAwareTag. The tag can then reference any beans defined within Spring.


    • #3
      Hi Marty

      My TagHandler is in a common library though and is not configured as a Bean so Im n ot sure RequestContextAwareTag will work will it since the TagHanlder will have no visibility of an ApplicationContext. I would have to configure the TagHandler as a separate bean in each war to use your approach wouldn't I?

      Advise appreciated.


      • #4
        What I am suggesting is to make your "common" TagHandler extend RequestContextAwareTag instead of TagSupport and then you can lookup the message resource within your tag handler. For instance:

        public class TestTag extends RequestContextAwareTag {
            private String messageCd;
            public void setMessageCd(String messageCd) {
                this.messageCd = messageCd;
            protected int doStartTagInternal() throws Exception {
                JspWriter out = pageContext.getOut();
                // get a reference to the message resource.        
                MessageSource messageSource = getRequestContext().getMessageSource();
                // dump out the value for the message code.
                out.write(messageSource.getMessage(messageCd, null, "Unknown", getRequestContext().getLocale()));


        • #5
          Ok, thanks will give it a go.

          cheers Marty.


          • #6
            Marty worked well - Thanks for your help.



            • #7
              Glad it worked for you.