论坛首页 Java企业应用论坛

Spring MVC 多 ViewResolver的疑惑

浏览 7884 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-02-13  
在Spring MVC 3.0 中 依然不支持 多个 ViewResolver 并存吗?

如果我在 config 里面有两个 ViewResolver 还只是支持最上面的, order 属性貌似部不管用。

@Configuration
public class AppConfig {
 
    @Bean
    ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setOrder(9);
        return resolver;
    }
    
    @Bean
    ViewResolver freemarkerViewResolver(){
    	FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
    	resolver.setCache(true);
    	resolver.setPrefix("WEB-INF/templates/");
    	resolver.setSuffix(".ftl");
    	//if you want to use the Spring FreeMarker macros, set this property to true
    	resolver.setExposeSpringMacroHelpers(true);
    	resolver.setOrder(1);
    	return resolver;
    }
    
    @Bean
    FreeMarkerConfig freemarkerConfig(){
    	FreeMarkerConfigurer config = new FreeMarkerConfigurer();
    	config.setTemplateLoaderPath("WEB-INF/templates/");
    	config.setDefaultEncoding("UTF-8");
    	Map<String,Object> variables = new HashMap<String,Object>();
    	variables.put("xml_escape", new XmlEscape());
    	variables.put("html_escape", new HtmlEscape());
    	config.setFreemarkerVariables(variables);
    	return config;
    }
    
}


所有的请求 都去找jsp了, 根本不去找ftl

1. 文档上说 如果第1个ViewResolver没有找到相应的view, 应该让第2个ViewResolver去找啊。
2. 文档上又说 只能存在一个 UrlBasedViewResolver, 但是我看我这两个ViewResolver 都是 继承于UrlBasedViewResolver。 所以说我的配置就是错误的????
3. 我在网上查找了一下 解决方案, 大多建议是 自己定义ViewResolver, 自己转发。 还有就是用 ResourceBundleViewResolver。

想看看大家都是 怎么解决这个 问题的 ??
难道 那个order属性就是个摆设 ?
   发表时间:2012-02-14  
支持吧。我都用得好好的
0 请登录后投票
   发表时间:2012-02-14  
楼主的头像好诱惑
0 请登录后投票
   发表时间:2012-02-14  
API中关于UrlBasedViewResolver有个notes
When chaining ViewResolvers, a UrlBasedViewResolver will check whether the specified resource actually exists. However, with InternalResourceView, it is not generally possible to determine the existence of the target resource upfront. In such a scenario, a UrlBasedViewResolver will always return View for any given view name; as a consequence, it should be configured as the last ViewResolver in the chain.
0 请登录后投票
   发表时间:2012-02-15   最后修改:2012-02-15
http://www.iteye.com/problems/76107
0 请登录后投票
   发表时间:2012-02-15  
yaolifei 写道
http://www.iteye.com/problems/76107

应该就是这个原因
0 请登录后投票
   发表时间:2012-02-15  
wwy_0918 写道
API中关于UrlBasedViewResolver有个notes
When chaining ViewResolvers, a UrlBasedViewResolver will check whether the specified resource actually exists. However, with InternalResourceView, it is not generally possible to determine the existence of the target resource upfront. In such a scenario, a UrlBasedViewResolver will always return View for any given view name; as a consequence, it should be configured as the last ViewResolver in the chain.


我看到了
引用

Spring supports more than just one view resolver. This allows you to chain resolvers and, for example, override specific views in certain circumstances. Chaining view resolvers is pretty straightforward - just add more than one resolver to your application context and, if necessary, set the order property to specify an order. Remember, the higher the order property, the later the view resolver will be positioned in the chain.


其实你没仔细看我的代码, 我已经设置了order了, 实际我的代码也是好用,
问题出在我测试的时候没有请websphere的缓存, 可能导致代码没有编译。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics