首页 文章

Spring Boot:使用thymeleaf处理一个没有控制器的html文件

提问于
浏览
2

我想用我的Spring Boot应用程序提供国际化的html页面,我正在使用Thymeleaf和 messages_XX.properties for i18n . 我更愿意将它们作为http://localhost:8080/some/path/test.html访问并将它们放在例如 /src/main/resources/html 但我默认情况下无法配置Spring Boot来使用百万富翁处理页面 .

作为临时解决方法,我有一个控制器

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

这对我来说很有用:http://localhost:8080/some/path/file.html进程并提供 src/templates/some/path/file.html 但是我可以配置一个地方 src/resources/html 将被百里香叶处理然后服务吗?

到目前为止我已经尝试过

spring.thymeleaf.prefix=classpath:/html/

application.properties 但它似乎对我不起作用 .

环境: spring-boot-starter-2.0.0.RELEASEspring-webmvc-5.0.4.RELEASEthymeleaf-spring5-3.0.9.RELEASE

1 回答

  • 0

    因为你正在切割.html:

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

    你可能应该设置

    spring.thymeleaf.suffix=.html
    

    或配置:

    @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;
    }
    

相关问题