Reputation: 5024
I'm having trouble sonfiguring Spring Boot to use thymeleaf and still serve static content. I have two directories in my resources dir: "/static" and "/templates". According to the Spring Boot documentation, thymeleaf should find thymeleaf templates in the templates directory by default. I am getting a 404 though when I try to use a template.
Relevant code from my Controller:
@RequestMapping("/")
public ModelAndView index() {
return new ModelAndView("index.html");
}
@RequestMapping(value = "/test")
public ModelAndView test() {
return new ModelAndView("test");
}
index.html is in resources/static and test.html is in resources/templates.
index.html works fine, but if you try to open /test in your browser, it throws a 404 saying that the thymeleaf template could not be found.
I really appreciate any help. I'm stumped.
Upvotes: 3
Views: 2709
Reputation: 1321
Have you tried just returning the String name of the template and not ModelAndview? And have your ModelAttributes annotated as indicated in the documentation?
Upvotes: 0
Reputation: 3572
Any pages that are not going through the ThymeleafViewResolver(your /resources/static) need to be removed.
/**
* Configures a {@link ThymeleafViewResolver}
*
* @return the configured {@code ThymeleafViewResolver}
*/
@Bean
public ThymeleafViewResolver thymeleafViewResolver()
{
String[] excludedViews = new String[]{
"/resources/static/*"};
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setOrder(1);
/*
* This is how we get around Thymeleaf view resolvers throwing an error instead of returning
* of null and allowing the next view resolver in the {@see
* DispatcherServlet#resolveViewName(String, Map<String, Object>, Locale,
* HttpServletRequest)} to resolve the view.
*/
resolver.setExcludedViewNames(excludedViews);
return resolver;
}
Upvotes: 1