百里香错误:org.springframework.expression.spel.SpelEvaluationException

时间:2018-10-31 10:34:00

标签: java spring-boot thymeleaf

期望

数据绑定后,网页应反映数据

详细信息

  

控制器代码

@Controller
@Configuration
@Component
public class Controller {

    @Autowired
    Credentials c;

    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("greeting", new RequestData());
        return "greeting";
    }

    @PostMapping("/greeting")

    public String greetingSubmit(@ModelAttribute RequestData greeting) {
        if (c.getUserName().equals(greeting.getId()) && c.getPassword().equals(greeting.getContent()))
        {
        return "result";
        }
        else
        {
            return "error";
        }
    }
}

RequestData.java

public class RequestData {

    private String id;
    private String content;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

Credentials.java

@Component
@Configuration
@PropertySource("classpath:application.properties")
public class Credentials {

    public String userName;
    public String password;

     @Autowired
     private Environment env;

    @Autowired
    public String getUserName() {

         return env.getProperty("spring.username");

            }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Autowired
    public String getPassword() {
         return env.getProperty("spring.password");

            }

    public void setPassword(String password) {
        this.password = password;
    }


}

result.html

 <!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> 
    <title>Hello World</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Hello World</h1>
    <p th:text="'id: ' + ${greeting.id}" />
    <p th:text="'content: ' + ${greeting.content}" />
    <a href="/greeting">Submit another message</a>
</body>
</html>

能够编译,打包代码库和构建jar。但是,当我运行jar文件时,出现以下错误:

2018-10-31 15:36:34.259 ERROR 8220 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[disp
atcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context
 with path [] threw exception [Request processing failed; nested exception is or
g.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringE
L expression: "greeting.id" (template: "result" - line 9, col 8)] with root caus
e

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property o
r field 'id' cannot be found on null

我不确定问题出在哪里,thymeleaf博士说,可以使用$ {attributeName}访问模型属性,并且我也使用相同的属性,但是为什么它说“找不到ID”

请提出建议

1 个答案:

答案 0 :(得分:1)

我从Spring MVC文档中引用-

  

未明确指定模型属性名称时会发生什么?   在这种情况下,会将默认名称分配给基于   在其类型上。例如,如果该方法返回的对象类型为   帐户,使用的默认名称是“帐户”。你可以改变   通过@ModelAttribute批注的值。如果添加   属性直接分配给模型,使用适当的重载   addAttribute(..)方法-即带有或不带有属性名称。

就您而言,

@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute("greeting) RequestData greeting) {
..
}