Accessing Spring MVC DI beans from jsp
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 som开发者_运维知识库e 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?
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.
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}
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.
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).
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
Annotation-based approach to the problem of usage of Spring beans in JSPs.
Spring MVC + JSP tiles:
@Bean
public ViewResolver tilesViewResolver() {
final UrlBasedViewResolver = new UrlBasedViewResolver();
...
viewResolver.setExposeContextBeansAsAttributes(true);
viewResolver.setExposedContextBeanNames("myBean");
return viewResolver;
}
And in my .jsp:
<c:set var="myBeanData" scope="request" value="${myBean.myMethod()}"/>
<a id="id" href="${myBeanData}">Link</a>
精彩评论