问题描述:
在一个springmvc项目中,我用了thymeleaf解析器和InternalResourceViewResolver解析器,我想让前者解析html页面,后者解析jsp页面,但是出现了以下情况:
一、当我设置前者的优先级大于后者时,html页面可以被前者正常解析,但是原本后缀名为jsp的j页面也会被解析成后缀为html,如图:
,就像后者解析器没有起到任何作用。
二、当我设置前者的优先级小于后者时,jsp页面可以正常解析,但是html页面被解析成了jsp后缀。
三、当两者优先级设置相同时,效果等同于第二种情况
项目源码:
package Configs;
Import...
@EnableAspectJAutoProxy
@EnableWebMvc
@Configuration
@ComponentScan("Controllers")
public class WebApplicationConfig {
@Bean
public InternalResourceViewResolver inter(){
InternalResourceViewResolver interr=new InternalResourceViewResolver();
// interr.setViewNames("jsp*");
interr.setPrefix("/Jsps/");
interr.setSuffix(".jsp");
interr.setOrder(1);
return interr;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver(@Autowired SpringTemplateEngine springTemplateEngine){
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setOrder(2);
resolver.setCharacterEncoding("UTF-8");
resolver.setTemplateEngine(springTemplateEngine);
return resolver;
}
@Bean
public SpringResourceTemplateResolver templateResolver(){
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML");
resolver.setPrefix("/HTMLS/");
return resolver;
}
@Bean
public SpringTemplateEngine springTemplateEngine(@Autowired ITemplateResolver resolver){
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(resolver);
return engine;
}
}
package Configs;
@Configuration
public class ZongConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebApplicationConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
package Controllers;
@Controller
public class Hello {
@RequestMapping("/hellohtml")
public String helloHtml(Model m){
m.addAttribute("title","hello,world");
return "hello";
}
}
package Controllers;
@Controller
public class HelloJsp {
@RequestMapping("/hellojsp")
public String getHelloJsp(){
return "hellojsp";
}
}