15

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?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Art79
  • 313
  • 2
  • 5
  • 16

6 Answers6

41

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 1
    Excellent answer :- ) Now once i exposed controller to jsp i can actually call it and get menu instance. Im not sure how am i going to do it at the end as i can see that coupling can increase fast but at least i have a way to do it! Thanks again! – Art79 Jan 15 '10 at 14:48
5

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}
dmlek
  • 51
  • 1
  • 1
3

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.

Eric
  • 5,137
  • 4
  • 34
  • 31
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).

madhead
  • 31,729
  • 16
  • 153
  • 201
0

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

les2
  • 14,093
  • 16
  • 59
  • 76
  • well fine, thats what im trying to figure out, how do i pass necessary things to my partial view files so i would not have to initiate them all in the same controller. My controller should not have to care about other ModelAndView properties. Ideally i would include partial (or call controller action) and it would invoke the controller responsible for it and prepare all the data from beans etc. then it would be passed to the partial view and rendered. At least i think that would work for me : -) – Art79 Jan 15 '10 at 14:22
  • If the partial views are all part of the same request-response cyhcle then the main view can set all model data for all partial views (as request params of course) – les2 Jan 15 '10 at 14:26
  • my main view is home.jsp and it includes menu.jsp. What do you mean by "set all model data"? do you mean something like this? The problem im having here is that i dont want to set ${menu} instance from every controller i can have. I would like to have this taken care of in a separate 'module'. i think there has to be some simple way to deal with this kind of use cases :- ) im just fresh to spring and cant see it :- ) – Art79 Jan 15 '10 at 14:33
  • sorry for not explaining more completely before -- I was typing on a phone!!! :) Now, IIRC, you can set all of the 'model' data necessary for rendering a view in the controller. You do this by adding them to the java.util.Map that functions as the 'model' in a ModelAndView. All of the Map's entries are exposed as request-scope attributes and made available to all JSPs that are part of that request-response cycle. So your controller should be able to just add all the data necessary for the all of the partial views, and then each partial view will render using the subset of attrs it requires. – les2 Jan 17 '10 at 05:17
  • I see that in the post you don't want to set all of the data for each of the non-related partial-views in your controller - this is for a good reason. I suppose you could have filters or interceptors that set the non-related attributes. that or some form of chaining (although chaining can quickly get out of hand). – les2 Jan 17 '10 at 05:20
0

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>
Shalguev
  • 81
  • 3