一,问题
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";
}