Spring MVC:几个ViewResolvers

时间:2014-01-05 18:18:26

标签: java spring java-ee tomcat spring-mvc

一个按预期工作,但我想有两个视图解析器,这可能吗?

<bean id="App_viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="order" value="1"/>
    <property name="prefix" value="/WEB-INF/views/App/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="OtherApp_viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="order" value="2"/>
    <property name="prefix" value="/WEB-INF/views/OtherApp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

??

我想通过调用

来呈现视图
new ModelAndView("start/start"); 

如果它存在于第一个视图解析器中,则渲染,否则尝试下一个。

InternalResourceViewResolver显然永远不会像其他地方所说的那样返回false,但是其他JSP视图解析器可以用于此吗?

扩展资源视图解析器?

1 个答案:

答案 0 :(得分:0)

你走了,这是答案:

import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Locale;

public class ViewResolver extends InternalResourceViewResolver {

    protected Class<?> requiredViewClass() {
        return View.class;
    }

    public static class View extends InternalResourceView {
        public boolean checkResource(Locale locale) throws Exception {
            if (getServletContext().getResource(getUrl()) != null ) {
                return true;
            }

            return false;
        }
    }
}


<bean id="App_viewResolver" class="package.ViewResolver">
    <property name="order" value="1"/>
    <property name="prefix" value="/views/App"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="OtherApp_viewResolver" class="package.ViewResolver">
    <property name="order" value="2"/>
    <property name="prefix" value="/WEB-INF/OtherApp"/>
    <property name="suffix" value=".jsp"/>
</bean>