I have a Spring MVC application with Thymeleaf view resolver. Here is my config class:
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();
}
}
Now I need to add a jsp resolver to the same application. I'm adding InternalResourceViewResolver bean but it doesn't work as expected:
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
Instead of getting test_jsp page when I go to /test/jsp I get an error:
@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";
}
Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/WEB-INF/templates/test_jsp]")
Does anybody know how to add jsp view resolver when ThymeLeaf is already in place? I need Spring to look for Thymeleaf first and when page is not found then he searches jsp. Thanks
InternalResourceViewResolver
? just like you didi withengine.setTemplateResolver(templateResolver());
but forjspViewResolver
. Thanx – Scaramouche