我有一个带有Thymeleaf视图解析器的Spring MVC应用程序。这是我的配置类:
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public SpringResourceTemplateResolver springResourceTemplateResolver() {
return new SpringResourceTemplateResolver();
}
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
@Bean
public TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.setTemplateResolver(templateResolver());
return engine;
}
private ITemplateResolver templateResolver() {
springResourceTemplateResolver().setPrefix("/WEB-INF/templates/");
springResourceTemplateResolver().setTemplateMode(TemplateMode.HTML);
return springResourceTemplateResolver();
}
}
现在我需要在同一个应用程序中添加一个jsp解析器。我正在添加InternalResourceViewResolver bean但它没有按预期工作:
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
当我转到/ test / jsp时,我得到一个错误,而不是获取test_jsp页面:
@RequestMapping(value = "/test/leaf", method = RequestMethod.GET)
public String mainPage() {
return "test_html.html";
}
@RequestMapping(value = "/test/jsp", method = RequestMethod.GET)
public String mainPage2() {
return "test_jsp";
}
请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateInputException:发生错误 在模板解析期间(模板:“ServletContext资源 [/ WEB-INF /模板/ test_jsp]“)
当ThymeLeaf已经到位时,是否有人知道如何添加jsp视图解析器?我需要Spring首先寻找Thymeleaf,当找不到页面时,他会搜索jsp。感谢
答案 0 :(得分:1)
将order
上的ThymeleafViewResolver
属性设置为比其他解析器上的其他order
属性更低的整数。零对你的情况有意义。
对于您的控制器,您只需要返回页面名称并省略扩展名(.html)。
最后,当您向SO发布错误消息时,发布完整跟踪。就目前而言,您的JSP也可能存在问题,但这里的读者无法轻易说出来。
答案 1 :(得分:0)
package com.mmm.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.mmm")
public class MainConfig implements WebMvcConfigurer {
@Autowired
WebApplicationContext webApplicationContext;
@Bean
public SpringResourceTemplateResolver getTemplateResolver() {
SpringResourceTemplateResolver templateResolver = new
SpringResourceTemplateResolver();
templateResolver.setApplicationContext(webApplicationContext);
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(getTemplateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Bean
ThymeleafViewResolver configureViewResolvers() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setOrder(1);
resolver.setViewNames(new String[] {"*.html"});
return resolver;
}
@Bean
public UrlBasedViewResolver getUrlBasedViewResolver() {
UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix("");
viewResolver.setOrder(2);
return viewResolver;
}