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