Spring中如何使用多个ViewResolver?
我正在开发一个 Web 应用程序,其中大部分页面都使用 apache 磁贴 (2.1.2),但其中一些只需是普通的 jsps。
我遇到的问题是 InternalResourceViewResolver
和 UrlBasedViewResolver
无论如何都会尝试解析视图,因此无论我使用哪种顺序,它都会失败在普通 JSP 页面或图块页面上。
这是配置:
<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>
为了更清楚地说明我想要做什么,我需要能够具有如下所示的视图状态:
<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?
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.
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.
Here is the config:
<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>
Does anyone know how to configure things so that I can get it to render tiles definitions and plain jsps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我通过简单地为普通 jsp 布局添加tile 定义解决了这个问题,如下所示:
然后您可以使用此布局作为包含简单 jsp 文件的模板。
和布局模板文件:
I resolved this issue by simply adding tiles definition for plain jsp's layout, like this:
Then you just can use this layout as template for including your simple jsp files.
And layout template file:
是的,您可以在项目中使用任意数量的视图解析器。
因此,您可以在同一项目中同时使用“tiles View resolver”和“Internal view resolver”。 。
您必须配置 ContentNegotiatingViewResolver 。 。
并在您的视图解析器中给出订单值。
就像我给了tiles视图解析器2和internalviewresolver3一样。它会首先检查tiles定义,如果在tiles中找不到视图,它将在InternaiViewResolver中检查,
这里是一些适合我的配置。
Yes you can use any number of view resolver in your your project.
So you can use both 'tiles View resolver' and 'Internal view resolver' in same project. .
you have to configure a ContentNegotiatingViewResolver . .
and give order value in your view resolvers.
like I have given tiles view resolver 2 and internalviewresolver 3. .It will first check in tiles definitions if a view is not found in tiles it will be checked in InternaiViewResolver
here is some configurations that works for me.
我刚刚通过将
*-servlet.xml
配置文件一分为二解决了同样的问题; 就我而言,主应用程序使用 Tiles,但我希望 QUnit 测试是简单的 JSP。app-servlet.xml
仅包含 Tiles 视图解析器,tests-servlet.xml
仅包含 JSP 视图解析器,web.xml
映射为根据 URL 将请求分派到正确的 servlet。I've just solved the same problem by splitting the
*-servlet.xml
config file in two; in my case the main application uses Tiles, but I want QUnit tests to be simple JSPs.app-servlet.xml
contains only the Tiles view resolver,tests-servlet.xml
only contains the JSP view resolver andweb.xml
mappings are dispatching requests to the correct servlet basing on the URL.看起来您走在正确的轨道上,但要记住的是,某些视图解析器的行为就好像它们始终解析了视图一样。 您需要确保将此类解析器放在订单的最后。 我相信 Tiles 观点就是这样的一种。
编辑:哎哟...是的,另一张海报是正确的,这两个解析器都会“始终匹配”,因此您不能在链中使用它们。 另一种替代方法是,如果找不到配置的平铺视图,则尝试扩展 TilesView 来执行简单的 JSP 渲染。
It looks like you're on the right track, but the thing to bear in mind is that some view resolvers behave as if they have always resolved the view. You need to make sure to put such resolvers last in your ordering. I believe the Tiles view is one such.
Edit: whoops... yes, the other poster is correct, both of these resolvers will do 'always match' so you can't use them both in a chain. Another alterative would be to try to extend the TilesView to do a simple JSP render if it cant find a configured tile view.
正如你所说,你不能将它们链接在一起。 两者的 javadoc 都明确指出它们必须都位于解析器链的末尾。
我建议,如果您确实需要一起使用它们,那么您可以编写一个简单的 ViewResolver 自定义实现,它采用视图名称,并决定委托给两个“真实”视图解析器中的哪一个。 这假设您可以根据视图名称判断要调用哪个解析器。
因此,您需要像这样定义一个自定义 ViewResolver:
您需要实现 isTilesView 方法来决定委托给哪个解析器。
在 XML 配置中,定义这个新的视图解析器,并确保它出现在其他视图解析器之前。
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.
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.
So you'd define a custom ViewResolver like this:
You'd need to implement the isTilesView method to decide which resolver to delegate to.
In the XML config, define this new view resolver, and make sure it appears before the other ones.