Accessing Spring MVC DI beans from jsp

22,460

Solution 1

Spring-MVC can expose the application context's beans to the view layer, if that is what you wish to do.

For example, the InternalResourceViewResolver can be instructed to expose every bean in the context, or just specified ones. Take a look at the exposeContextBeansAsAttributes and exposedContextBeanNames properties.

For example, say you wanted to expose the beans beanA and beanB to your JSPs. You would declare the view resolver in your context thus:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="exposedContextBeanNames">
      <list>
         <value>beanA</value>
         <value>beanB</value>
      </list>
   </property>
</bean>

Alternatively, to just expose every bean:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="exposeContextBeansAsAttributes" value="true"/>
</bean>

Whether or not this is a good idea is another question, but Spring does give you the option.

Solution 2

IMO add use exposedContextBeanNames in viewResolver bean configuration (there is also possibility to use global flag and it's rather not recommended)

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="exposedContextBeanNames">
 <list>
  <value>beanName</value>
 </list>

use in your JSP

${beanName.property}

Solution 3

You can use spring:eval tag:

...
<spring:eval expression="@properties.getProperty('myProp')" var="myProp" />
${myProp}
...

Where @properties is a bean name of you properties in Spring Context. Note that this approach does not use exposedContextBeanNames, so it can be used with Tiles Views, for example (TilesViewResolver does not have such propery).

Solution 4

A critical part of using InternalResourceViewResolver seems to be that Spring needs to be involved in the code flow when the jsp page is being processed. If you're accessing the jsp page directly or otherwise bypassing any Spring-based action (e.g. perhaps by internally forwarding to a jsp page due to login configuration in web.xml), then that won't work.

However, it is possible to configure your application so that certain beans are accesible to anything that can get to the ServletContext (aka applicationScope) through the use of the ServletContextAttributeExporter class.

In your spring configuration, add:

<bean id="mybean" .../>
<bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
    <property name="attributes">
        <map>
            <entry key="attrname" value-ref="mybean"/>
        </map>
    </property>
</bean>

Then, in a JSP page you could access that bean with:

${applicationScope.attrname}

or if you know you don't have an "attrname" attribute in a more specific scope, just:

${attrname}

Obviously this won't be able to refer to request or session scope beans, but if you need access to a singleton bean then it works great.

Share:
22,460
Art79
Author by

Art79

Born developer. Born in Poland, living in Sydney after 5 years in Ireland. Web technologies in general (PHP is my specialty).

Updated on May 23, 2021

Comments

  • Art79
    Art79 almost 3 years

    In some MVC frameworks you can call controller action from the view if you wish to execute some code and render some partial view. I'm not sure what is the correct way to do it in Spring MVC

    I want to have set of JSP templates. Some of them will be page layouts some of them will be small components like paginator, login box, menu, tag cloud etc etc etc. Each of these component need some beans or controller action to set some data into ViewAndModel so that view could use it.

    The problem is I don't want to set all these objects in each call. My register controller cares only about registration processing. So now how do i do it right? How do I call DI beans or controllers from the view to prepare partial views? Or should I create some mappings? Or am I approaching the problem from totally wrong angle?