展示表单@postMapping EL1007E: Property or field ‘name‘ cannot be found on null

问题描述

目标: 在页面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
}

完结撒花

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值