Spring Boot 生产环境模板引入错误

本文介绍在SpringBoot项目中使用Thymeleaf模板引擎时,遇到的模板加载失败问题及其解决方案。通过调整配置文件和Controller返回路径,确保模板在不同运行环境下正确加载。

生产环境引入模板报错

使用SpringBoot 推荐的模板引擎Thymeleaf时,在Eclipse中运行正常,但打成Jar包运行时,报错:
template might not exist or might not be accessible by any of the configured Template Resolvers

ERROR (TemplateEngine.java:1085)- [THYMELEAF][http-nio-8
080-exec-10] Exception processing template "/user/userlist": Error resolving template "/user/userlist", template might not exist or might not be accessible by any of the configured Template Resolvers
2018-03-26 11:03:10,444 ERROR (DirectJDKLog.java:181)- Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request proces
sing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/user/userlist", template might not exist or might n
ot be accessible by any of the configured Template Resolvers] with root cause org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/user/userlist", template might not exist or might not be accessible by any of the configured Template Resolvers
        at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246)
        at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
        at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
        at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
        at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
        at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)
        at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286)
        at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)

解决方案

修改application.properties 文件
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
修改Controller类的返回路径
@RequestMapping("/user/list")
public String toList() {
    return "user/userlist";
}

其中:
return “/user/userlist”; //在eclipse中运行正常,打成jar找不到模板
return “user/userlist”;//在eclipse中运行正常、Jar形式运行正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值