Announcement Announcement Module
No announcement yet.
Jsp useBean to retrieve Spring managed bean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Jsp useBean to retrieve Spring managed bean

    Is there something available to retrieve a Spring managed bean in JSP.

    My use case is that I want to use a bean in tag file.

  • #2
    I'd like to have answer to that as well. Seems like it should be a simple thing to do.

    In the mean time, you can always just put what you need in the Model with your Controller and access it with ${bean} in your jsp.


    • #3
      First off all I think that it is a bad practice to access spring managed beans in your jsp. But if you really must you can use a scriptlet which uses the RequestContext utils to get the ApplicationContext and then you can get a bean from there.

      ApplicationContext ac = RequestContextUtils.getWebApplicationContext(request);
      YourBean bean = (YourBean) ac.getBean("yourBeanName");
      Another solution would be to configure a that way you could retrieve the beans from the servletContext instead of the ApplicatioNContext not tying your jsp to spring.

      However the best solution imho and the way MVC should be used is to populate your Model correctly and only let the jsp render that model.


      • #4
        Thanks, wasn't aware of a ServletContextAttributeExporter.

        I was thinking of writing a listener which would do the say, but this is even better.