Alexey Kutuzov
Alexey Kutuzov

Reputation: 689

How to use placeholder for controller class name in ViewReolver

I want to put controller name (class name or bean name) as dir name when SpringMVC is resolving view name. I defined prefix param in UrlBasedViewResolver like /WEB-INF/admin/${controller}/, but it doesn't work, of course.

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="prefix" value="/WEB-INF/admin/${controller}/"/>
    <property name="suffix" value=".jsp"/>
    <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/>
</bean>

Obviously, it doesn't work because UrlBasedViewResolver just simple attach view prefix to view name (like view.setUrl(getPrefix() + viewName + getSuffix());).

What's the easiest way to implement this issue?

Or what's the easiest way to get controller name in resolver to overried buildView method in UrlBasedViewResolver subclass?

Upvotes: 0

Views: 236

Answers (1)

axtavt
axtavt

Reputation: 242686

You can try to create a HandlerInterceptor and modify view name property of ModelAndView in its postHandle() method (it's invoked after execution of controller but before rendering a view). This method also receives instance of the controller as handle.

Upvotes: 1

Related Questions