Java thymeleaf错误:org.springframework.expression.spel.SpelEvaluationException
预期的内容 数据绑定后,网页应该反映数据 详细信息 控制器代码 RequestData.javaJava thymeleaf错误:org.springframework.expression.spel.SpelEvaluationException,java,spring-boot,thymeleaf,Java,Spring Boot,Thymeleaf,预期的内容 数据绑定后,网页应该反映数据 详细信息 控制器代码 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 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>
我不确定问题在哪里,thymeleaf doc说,模型属性可以通过${attributeName}访问,我也使用的是相同的,但是为什么它说“找不到id”
请从Spring mvc文档中提出建议,我引用- 如果没有显式指定模型属性名称,会发生什么情况? 在这种情况下,将根据模型属性指定默认名称 关于它的类型。例如,如果该方法返回类型为的对象 Account,使用的默认名称为“Account”。你可以改变 通过@modeldattribute注释的值。如果添加 属性直接添加到模型中,请使用适当的重载 addAttribute(..)方法-即,有或没有属性名称 就你而言
@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute("greeting) RequestData greeting) {
..
}
是否确实调用了greetingForm方法?
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
@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute("greeting) RequestData greeting) {
..
}