0
votes

I created Spring MVC with Tiles project.

My controller returns a string "hello" which is a logical file name and I have jsp called as hello.jsp.

In tiles.xml I should have a definition named hello which extends a definition template. My basic definition is mapped to layout.jsp.

When I add the jspViewResolver, it takes me to hello.jsp, but if I comment it out it takes me to layout.jsp which is rendered based on the definition of hello in tiles.xml.

So, why we shouldn't have both jspViewResolver and tilesViewResolver together?

1
Both are instances of a UrlBasedViewResolver which simple construct a URL regardless of the actually existence of the view being request. Other ViewResolver like the BeanNameViewResolver don't return a view when it doesn't exists. Having multiple UrlBasedViewResolvers in your view resolver chain will make the last ones never called. As explained in the documentation of the class.M. Deinum
If you would have put this as an answer, I might have selected this as best answer. :)sofs1
The comment wasn't actually fully true, gave a bit more elaborate explanation.M. Deinum

1 Answers

1
votes

Both the TilesViewResolver and InternalResourceViewResolver or instances instances of a UrlBasedViewResolver. In general this means that the ViewResolver takes the given view name and tries to construct a URL out of it this URL is used to resolve a view, regardless of the actual existence of the view.

Depending on the Spring version (Spring >= 3.0) and used subclasses a check will be done to check the actual existence of the view. (See this solved issue).

The only implementation not actually checking the existence of views (at the moment) it the InternalResourceViewResolver or plain UrlBasedViewResolver instances. When using this make this the last one (highest order property) in the chain of view resolver.

When you want to combine plain JSP with Tiles this should actually be possible by giving the TilesViewResolver an order of 1 and the InternalResourceViewResolver an order higher then 1 (2 for instance).

In earlier versions (Spring < 3.0) these checks aren't in place and the ordering will as such not have any effect.