views:

16

answers:

1

Hi all,

I need to change some existing application to render different view depending on some request parameter. This application is using tiles. I never used tiles before, and need a bit of hint how to go about doing this.

Tiles are defined as follows:

<bean id="viewResolver"class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" /></bean>

Let's say I have welcome.tile, if there is some parameter in request eg. id=xyz and tile xyz.welcome.tile exists I would like to render this one, if it does not exist, I would like to render default. My controller returns welcome.tile as view, but I do not want to add code to controllers, I would like to do this as simply as possible.

My thought was to somehow intercept view resolution so it will check parameter on request and then verify if given tile with prefix from parameter exists, if it exists it would be used. But I am not sure where to start.

This is some old code, so unfortunately I cannot afford for to much changes.

Thanks in advance Konrad

+1  A: 

I think you can achieve that extending UrlBasedViewResolver with a version that reads the request parameter of your wish and build the view name with this information. Knowing that ViewResolver interface doesn't provide an instance of HttpRequest, you can get it using the org.springframework.web.util.RequestHolder.currentRequest() static method.

This way if you request is (for example) /mvc/dailyReport/criticalStuff, the viewName can be dailyReport.criticalStuff

and if you request is /mvc/dailyReport/criticalStuff?printable, the viewName can be resolved as dailyReport.criticalStuff.printable where this is a printable version of the dailyReport.criticalStuff view.

Hope it helps

ejmarino