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

  • Get ApplicationContext in JSP? ResourceContext?

    Hi,



    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?


    cheers

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

    Comment


    • #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.
      Jon

      Comment


      • #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:

        Code:
        public class TestTag extends RequestContextAwareTag {
        
            private String messageCd;
        
            public void setMessageCd(String messageCd) {
                this.messageCd = messageCd;
            }
            
            @Override
            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()));
            }
        }

        Comment


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



          cheers Marty.

          Comment


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

            Jon

            Comment


            • #7
              Glad it worked for you.

              Comment

              Working...
              X