Accessing Spring MVC DI beans from jsp

2019-01-13 11:39发布

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?

5条回答
Ridiculous、
2楼-- · 2019-01-13 11:54

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).

查看更多
萌系小妹纸
3楼-- · 2019-01-13 11:56

Never access business components from jsp views; something like sitemesh can be used to combine mutiple views in one. Jsps should also not invoke controller methods directly

查看更多
等我变得足够好
4楼-- · 2019-01-13 11:57

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.

查看更多
淡お忘
5楼-- · 2019-01-13 12:01

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.

查看更多
Fickle 薄情
6楼-- · 2019-01-13 12:06

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}
查看更多
登录 后发表回答