You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring+Thymeleaf表单提交遇EL1007E空指针错误求助

问题详情

刚掌握Spring+Thymeleaf实现简单表单提交与结果展示的方法,将该逻辑复用至数月前创建的带CSS样式的复杂表单时,触发EL1007E: 无法在null对象上找到'name'属性错误。在SurveyResults.html中添加空检查(如${surveyData?.name})后,页面可正常加载,但所有字段(姓名、年龄、邮箱等)均显示为空。


相关代码

Controller代码

// 实际Controller代码示例
@Controller
public class SurveyController {
    @GetMapping("/survey")
    public String showSurvey() {
        return "Survey";
    }

    @PostMapping("/submit")
    public String submitSurvey(SurveyData surveyData) {
        return "SurveyResults";
    }
}

Survey页面(Survey.html)

<!-- 实际表单页面代码示例 -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/css/survey.css}">
</head>
<body>
    <form th:action="@{/submit}" method="post">
        <input type="text" name="name" placeholder="姓名">
        <input type="number" name="age" placeholder="年龄">
        <input type="email" name="email" placeholder="邮箱">
        <button type="submit">提交</button>
    </form>
</body>
</html>

CSS样式(survey.css)

/* 实际CSS代码示例 */
form {
    max-width: 500px;
    margin: 2rem auto;
    padding: 1rem;
    border: 1px solid #ddd;
}
input {
    display: block;
    width: 100%;
    margin: 0.5rem 0;
    padding: 0.5rem;
}

SurveyData实体类

// 实际实体类代码示例
public class SurveyData {
    private String name;
    private Integer age;
    private String email;

    // 未添加Getter/Setter方法会导致参数绑定失败
}

结果页面(SurveyResults.html)

<!-- 实际结果页面代码示例 -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <h1>调查结果</h1>
    <p>姓名: <span th:text="${surveyData.name}"></span></p>
    <p>年龄: <span th:text="${surveyData.age}"></span></p>
    <p>邮箱: <span th:text="${surveyData.email}"></span></p>
</body>
</html>

报错堆栈信息

// 实际报错堆栈示例
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)
    ...

解决方法

1. 修正Controller的参数绑定与Model传递

  • 跳转表单页面时,必须初始化SurveyData对象并放入Model,避免Thymeleaf渲染表单时找不到绑定对象:
    @GetMapping("/survey")
    public String showSurvey(Model model) {
        model.addAttribute("surveyData", new SurveyData());
        return "Survey";
    }
    
  • 处理表单提交时,用@ModelAttribute接收参数,并将对象传入Model供结果页面使用:
    @PostMapping("/submit")
    public String submitSurvey(@ModelAttribute SurveyData surveyData, Model model) {
        model.addAttribute("surveyData", surveyData);
        return "SurveyResults";
    }
    

2. 完善表单的Thymeleaf绑定

  • 表单标签必须指定th:object="${surveyData}",明确绑定的实体对象:
    <form th:action="@{/submit}" th:object="${surveyData}" method="post">
    
  • 输入字段使用th:field替代原生name属性,确保与实体类属性精准绑定:
    <input type="text" th:field="*{name}" placeholder="姓名">
    <input type="number" th:field="*{age}" placeholder="年龄">
    <input type="email" th:field="*{email}" placeholder="邮箱">
    

3. 确保实体类拥有完整的Getter/Setter方法

Spring MVC依赖Getter/Setter实现参数绑定,必须为每个属性添加对应方法:

public class SurveyData {
    private String name;
    private Integer age;
    private String email;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public Integer getAge() { return age; }
    public void setAge(Integer age) { this.age = age; }

    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
}

4. 检查结果页面的对象引用

结果页面中使用${surveyData.name}时,需确保Model中传入的属性名与页面引用完全一致,空检查仅作为容错,核心是保证对象已正确传入Model。

内容的提问来源于stack exchange,提问作者Devayan Mitra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 04:07:02