Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring and view resolvers

is it possible to use InternalResourceViewResolver and BeanNameViewResolver together in the same web app?

I mean InternalResourceViewResolver to resolve my jsp.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

and instead BeanNameViewResolver to resolve my excel (I need to pass the url).

Bye. Thanks

like image 679
andrew0007 Avatar asked Mar 02 '10 22:03

andrew0007


2 Answers

You can have as many view resolvers in your context as you like, Spring will go over them one by one until it fins one that resolves the view.

There is one big caveat, though:

Note: When chaining ViewResolvers, an InternalResourceViewResolver always needs to be last, as it will attempt to resolve any view name, no matter whether the underlying resource actually exists.

This is a "flaw" with the servlet API, since the InternalResourceViewResolver has no way of knowing if the resource exists before actually trying it.

So make sure your BeanNameViewResolver is defined before the InternalResourceViewResolver, or explicitly specify the order property on each one.

like image 87
skaffman Avatar answered Oct 27 '22 19:10

skaffman


Yes, you can combine multiple resolvers. Spring iterates over them and uses the first resolver which is able to resolve the given name. You can also set the order property in resolvers to specify the order of iteration.

like image 40
axtavt Avatar answered Oct 27 '22 19:10

axtavt