views:

884

answers:

2

Im bit confused so be patient if im trying to do something wrong : -)

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

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 dont want to set all these objects in ecach call. My register controller cares only about regstration 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?

A: 

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
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
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
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?<c:set var="menuItem" value="${menu}" /><jsp:include page="menu.jsp"></jsp:include>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
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
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
+6  A: 

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
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
You can thank me by voting me up and accepting the answer :)
skaffman