views:

420

answers:

2

Hi,

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

+2  A: 

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.

axtavt
+4  A: 

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.

skaffman
Thank you!The order attribute is needed only if I'm in the same servlet context. I mean if I have a servlet context for the jsp where I'm using InternalResourceViewResolver and another for the excel files where I'm using BeanNameViewResolver I don't need to use the order.Right? They are independent..
andrew007
That's right. The `order` attribute is required only if they're in the same context, *and* they're not already in the right physical order in the file.
skaffman