Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use different view resolvers?

I have multiple view resolvers in a Spring configuration and wanted to use different view resolvers for different requests.

Example: For URLs starting with report_*, use Birt view resolver, and for ajax calls use Tiles resolver and so on.

Tried setting order property, but all views are resolved by tilesViewResolver.

<beans:bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">     <beans:property name="viewClass" value="com.example.example.util.AjaxTiles21View"/> </beans:bean>  <beans:bean id="birtViewResolver" class="org.eclipse.birt.spring.core.BirtViewResolver">     ...     <beans:property name="order" value="2" /> </beans:bean>  <beans:bean id="beanNameResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">     <beans:property name="order" value="1" /> </beans:bean> 
like image 512
tangobee Avatar asked Aug 02 '14 04:08

tangobee


People also ask

Can we use multiple view resolvers in Spring MVC?

In case you want to use a Multiple View Resolver in a Spring MVC application then priority order can be set using the order property. The following example shows how to use the ResourceBundleViewResolver and the InternalResourceViewResolver in the Spring Web MVC Framework.

What are the different types of view resolver?

Below, we will discuss about three important View Resolver implementations provided by Spring MVC, InternalResourceViewResolver , XmlViewResolver and ResourceBundleViewResolver .

What is the use of view resolver?

The ViewResolver provides a mapping between view names and actual views. The View interface addresses the preparation of the request and hands the request over to one of the view technologies.

What is the default view resolver in Spring MVC?

The default is an automatically-registered InternalResourceViewResolver ( UrlBasedViewResolver is an abstract superclass of this).


1 Answers

You absolutely can. ViewResolver has a single method, resolveViewName(String) which returns

the View object, or null if not found (optional, to allow for ViewResolver chaining)

Your ViewResolver beans are registered. When a view name is returned by a handler, Spring will iterate through each ViewResolver, invoking their resolveViewName method with the given name. If the method returns non-null, that View is used. If null is returned, then it continues iterating.

So the implementation has to return null if Spring is to skip it.

There are some implementations that never return null. This seems to be the case with your custom ViewResolver classes. If the ViewResolver returns a View, even if that View will eventually fail to be rendered, Spring will use it.

You either need to fix that or order your ViewResolver beans. For example, you can order them with the Ordered interface. Have your classes implement that interface and return an appropriate value.

like image 111
Sotirios Delimanolis Avatar answered Sep 18 '22 18:09

Sotirios Delimanolis