看到哪学到哪系列之Springboot整合Thymeleaf遇到的问题

17 篇文章 0 订阅
5 篇文章 0 订阅

Springboot整合Thymeleaf遇到的问题

一,问题

1,背景

@Controller
public class HomeController {
    @Autowired
    private IndexService indexService;
    @GetMapping({"/ ","/home"})
    public String showIndex(){
        return "calculation";
    }
<h1>个人所得税计算器</h1>
    <form th:action="@{/calculate}" th:object="${calculation}" method="post">
        <p>税前工资:<input type="text" th:field="*{wage}"/></p>
        <p>应纳税所得额:<input type="text" th:field="*{wagemore}"/></p>
        <p>税率:<input type="text" th:field="*{cess}"/></p>
        <p>速算扣除数:<input type="text" th:field="*{preminus}"/></p>
        <p>扣税额:<input type="text" th:field="*{wageminus}"/></p>
        <p>税后工资:<input type="text" th:field="*{actualwage}"/></p>
        <p><input type="submit" value="计算" /></p>
    </form>

2,描述

Springboot整合Thymeleaf时候,会遇到以下的错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Sep 24 16:53:46 CST 2021
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/calculation.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/calculation.html]")
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
	at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
	......

意思是解析html文件的时候失败了!

3,原因

在加载html页面的时候,表单中存在th:object="${calculation}",所以在加载时需要从后台传给这个object一个对象【空的也行】,如果不传,该object就会因为初始化失败而导致页面的解析失败!

一天一个当,当当不一样!!!

4,正确的做法

@GetMapping({"/ ","/home"})
public String showIndex(Calculation calculation){
    return "calculation";
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值