如何在Spring中使用多个ViewResolvers? [英] How to use multiple ViewResolvers in Spring?

查看:137
本文介绍了如何在Spring中使用多个ViewResolvers?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Web应用程序,其中我的大部分页面都使用apache tile(2.1.2),但是其中一些仅是纯jsps.

I am working on a web app where I have most of my pages making use of apache tiles (2.1.2), but a few of them need to just be plain jsps.

我遇到的一个问题是,无论如何InternalResourceViewResolverUrlBasedViewResolver都将尝试解析视图,因此无论我使用哪种顺序,它都将在纯JSP页面上失败,或者在图块页面上.

I am having a problem in that both an InternalResourceViewResolver and a UrlBasedViewResolver will try to resolve the view no matter what, so that no matter which ordering I use, it will either fail on the plain JSP pages, or on the tiles pages.

这是配置:

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

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
    <property name="order" value="1"/>
</bean>

为了更清楚地说明我要执行的操作,我需要能够具有如下所示的视图状态:

To make it more clear what I am trying to do, I need to be able to have view states like this:

<view-state id="someState" view="/someDir/foo"><!--render foo.jsp -->
    <transition on="foo" to="bar"/>
</view-state>

<view-state id="someState" view="something.core"><!--render tile defintion named 'something.core' -->
    <transition on="foo" to="bar"/>
</view-state>

有人知道如何配置东西,以便我可以渲染图块定义和纯jsps吗?

Does anyone know how to configure things so that I can get it to render tiles definitions and plain jsps?

推荐答案

正如您所说,您不能将它们链接在一起.两者的javadoc都明确指出,它们都必须都在解析程序链的末尾.

As you say, you cannot chain these together. The javadoc for both states clearly that they must both be at the end of the resolver chain.

我建议,如果您确实需要使用这些集合,那么您可以编写一个简单的ViewResolver自定义实现,该实现采用视图名称,并确定要委派给您的两个实际"视图解析器中的哪个.假设您可以根据视图名称确定要调用哪个解析器.

I suggest that if you really need to use these togather, then you write a simple custom implementation of ViewResolver which takes the view name, and decides which of your two "real" view resolvers to delegate to. This assumes that you can tell which resolver to call based on the view name.

因此,您将像这样定义自定义ViewResolver:

So you'd define a custom ViewResolver like this:

public class MyViewResolver implements ViewResolver {

    private ViewResolver tilesResolver;
    private ViewResolver jspResolver;

    public void setJspResolver(ViewResolver jspResolver) {
        this.jspResolver = jspResolver;
    }

    public void setTilesResolver(ViewResolver tilesResolver) {
        this.tilesResolver = tilesResolver;
    }

    public View resolveViewName(String viewName, Locale locale) throws Exception {
        if (isTilesView(viewName)) {
            return tilesResolver.resolveViewName(viewName, locale);
        } else {
            return jspResolver.resolveViewName(viewName, locale);
        }
    }

    private boolean isTilesView(String viewName) {
    .....
    }
}

您需要实现isTilesView方法来确定要委派给哪个解析器.

You'd need to implement the isTilesView method to decide which resolver to delegate to.

在XML配置中,定义此新视图解析器,并确保它出现在其他视图解析器之前.

In the XML config, define this new view resolver, and make sure it appears before the other ones.

<bean class="MyViewResolver">
    <property name="tilesResolver" ref="tilesViewResolver"/>
    <property name="jspResolver" ref="viewResolver"/>
</bean>

这篇关于如何在Spring中使用多个ViewResolvers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆