问题描述
目标: 在页面losing.html使用表单接收数据,然后在页面resultForLost.html中展示。
@Controller
public static class forLostController {
@GetMapping("/losing")
public String lostingForm(Model model) {
model.addAttribute("losing", new lostUser());
return "losing";
}
@PostMapping("/losing")
public String losingSubmit(@ModelAttribute lostUser losing) throws BrokerException {
//it works!!
System.out.println(losing.getName());
System.out.println(losing.getHeight());
//something wrong while showing the page "resultForLost.html"
return "resultForLost";
}
}
对应html展示:
<span class="visible-xs"><p th:text="'姓名: ' + ${losing.name}" /></span>
Idea报错,控制台输出:
2020-10-13 21:30:54.485 ERROR 7941 --- [nio-9998-exec-3] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-9998-exec-3] Exception processing template "resultForLost": Exception evaluating SpringEL expression: "losing.name" (template: "resultForLost" - line 30, col 52)
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "lostng.name" (template: "resultForLost" - line 30, col 52)
...
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
...
2020-10-13 21:30:54.486 ERROR 7941 --- [nio-9998-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "losing.name" (template: "resultForLost" - line 30, col 52)] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
因为没接触过spring-boot,直接上手找代码,找示例,用的示例就是上面的,一直不知道什么原因,看了springMVC教程,改了一点点就可以运行了…
@PostMapping("/finding")
public String findingSubmit(@ModelAttribute("losing") lostUser losing) throws BrokerException {
//code
}
完结撒花