2

I would like to serve internationalized html pages with my Spring Boot application and I am using Thymeleaf with messages_XX.properties for i18n. I would prefer accessing these as http://localhost:8080/some/path/test.html and having them in e.g. /src/main/resources/html but I am unable to configure Spring Boot to process a page using thymeleaf by default.

As a temporary workaround I have a controller with

@RequestMapping(value="**/*.html")
public String serve(HttpServletRequest req) {
    String res = req.getRequestURI().substring(0, req.getRequestURI().length() - 5);
    res = res.substring(1);
    return res;
}

This works for me now: http://localhost:8080/some/path/file.html processes and serves src/templates/some/path/file.html but can I just configure somewhere that src/resources/html are to be PROCESSED by thymeleaf and then served?

So far I have tried

spring.thymeleaf.prefix=classpath:/html/

in application.properties but it does not seemed to work for me.

Environment: spring-boot-starter-2.0.0.RELEASE, spring-webmvc-5.0.4.RELEASE, thymeleaf-spring5-3.0.9.RELEASE

HingeSight
  • 400
  • 5
  • 13

1 Answers1

0

Since you are cutting of the .html in:

String res = req.getRequestURI().substring(0, req.getRequestURI().length() - 5);

You should probably set

spring.thymeleaf.suffix=.html

Or with Configuration:

@Bean
public SpringResourceTemplateResolver templateResolver(){
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setApplicationContext(this.applicationContext);
    templateResolver.setPrefix("classpath:/html/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateMode.HTML);
    templateResolver.setCacheable(true);
    return templateResolver;
}
  • I do not mind cutting the .html part. I do not want to have this controller in the first place. Anyway, seems I will have to live with it. – HingeSight Apr 06 '18 at 13:59