양식 제출시 변수를 결과 페이지로 전달하려고 시도하지만 매번 null을 반환합니다. 아무리 많은 유사 콘텐츠를 사용해 보았습니다.Thymeleaf 템플릿의 자리 표시자가 null에 없음
그래서 내가 얻을 login.html에 대한 사후지도 :
@GetMapping("/login")
public String greetingForm(Model model) {
model.addAttribute("User2", new User());
return "login";
}
@PostMapping("/login")
public String greetingSubmit(@ModelAttribute User regUser) {
//userRepository.save(regUser);
//String teststr = regUser.getName();
//System.out.println(teststr);
return "result";
}
실제 Login.html은 다음과 같습니다
User.java 파일의<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="@{/demo/login}" th:object="${User2}" method="post">
<p>Username: <input type="text" th:field="*{name}" /></p>
<p>Password: <input type="text" th:field="*{password}" /></p>
<p>Balance: <input type="text" th:field="*{balance}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
부분 :
@Entity // This tells Hibernate to make a table out of this class
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String password;
private int balance;
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
이며 result.html은
입니다.이 계속 반복이 라인에 null 오류를 던지고 :
<p th:text="'username: ' + ${User.name}" />
특히 ${User.name}
, 보이는
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
나는 그래서 내가 해봤 모든 것을 생각할 수
같은 of,01 23,516,${User2.name} ${User} ${User2} ${regUser.name} ${teststr} ${#User.name}
등 .... 내가
System.out.println(regUser.getName());
의 주석을 경우는 사용자의 이름이 잘 인쇄 할 수 있습니다 매우 이상하다,하지만 변수를 전달하지 않습니다. 내 자리 표시 자입니까 아니면
User
개체이기 때문에th:text
으로 전달할 수 없나요? 제발 제가 아주 혼란스럽게 도와주세요!편집 : 자습서 I 따라 (https://spring.io/guides/gs/handling-form-submission/) 자습서를 복사하면 잘 작동하지만 문자열 만 전달합니다. User.name은 문자열 변수가 되겠지만 ...