I am new to spring 5 and spring boot. I am trying to create a spring 5/spring boot application with thymeleaf. I want to creates a war rather than use the embedded webserver with spring boot.
When I deploy my war, my application starts and I can access test html pages in src/main/resources/static/ with javascript in them which call my controllers. I can perform round trips on these pages to my controller and db.
However, I get a 404 when I try to open a thymeleaf page located at src/main/resources/templates/testtemplate.html
Relevant maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
Application:
@SpringBootApplication(exclude = {HibernateJpaAutoConfiguration.class, DataSourceAutoConfiguration.class})
public class WarApplication extends SpringBootServletInitializer
{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
{
return application.sources(WarApplication.class);
}
public static void main(String[] args) throws Exception
{
SpringApplication.run(WarApplication.class, args);
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(DataServiceConfig.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(WebConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
WebConfig:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.myproject")
public class WebConfig implements WebMvcConfigurer
{
@Autowired
ApplicationContext ctx;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
@Bean
@Description("Thymeleaf Template Resolver")
public SpringResourceTemplateResolver templateResolver()
{
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver ();
templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
return templateResolver;
}
@Bean
@Description("Thymeleaf Template Engine")
public SpringTemplateEngine templateEngine()
{
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
templateEngine.setTemplateEngineMessageSource(messageSource());
return templateEngine;
}
@Bean
@Description("Thymeleaf View Resolver")
public ThymeleafViewResolver viewResolver()
{
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(1);
return viewResolver;
}
@Bean
@Description("Spring Message Resolver")
public ResourceBundleMessageSource messageSource()
{
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
In the web config, if I remove the addResourceHandlers method it changes nothing. I can still find my static pages at localhost:8080/.
I tried adding this line to it: registry.addResourceHandler("/**").addResourceLocations("classpath:/templates/");
When I do I can then access the thymeleaf template at localhost:8080/mytemplate.html. However, it appears as a static page. The fragments are not translated. The "th" tags appear to be ignored.
I also tried removing the templateResolver, viewResolver and templateEngine beans from my webconfig as I thought maybe I was overwriting some automatic configuration. This didn't have any effect.
I believe my directory structure is fairly standard:
src/main/
/java/com/myproject/[code here]
/resources/static[web pages here]
/resources/templates[thymeleaf pages here]
What am I missing?
I am a total newbie with modern spring. So I might be doing somethign boneheaded. All this autoconfiguration in springboot stuff gets really frustrating as I can't figure out how to debug what it is doing.