2017-09-22 1 views
-1

양식 제출시 변수를 결과 페이지로 전달하려고 시도하지만 매번 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,

${User2.name} 
${User} 
${User2} 
${regUser.name} 
${teststr} 
${#User.name} 
01 23,516,

등 .... 내가 System.out.println(regUser.getName()); 의 주석을 경우는 사용자의 이름이 잘 인쇄 할 수 있습니다 매우 이상하다,하지만 변수를 전달하지 않습니다

. 내 자리 표시 자입니까 아니면 User 개체이기 때문에 th:text으로 전달할 수 없나요? 제발 제가 아주 혼란스럽게 도와주세요!

편집 : 자습서 I 따라 (https://spring.io/guides/gs/handling-form-submission/) 자습서를 복사하면 잘 작동하지만 문자열 만 전달합니다. User.name은 문자열 변수가 되겠지만 ...

답변

0

@ModelAttribute의 요소는 name입니다. 그 javadoc 상태

바인딩 할 모델 속성의 이름.

기본 모델 이름 비 클래스 이름에 기초하여 선언 속성 유형 (즉, 메소드 파라미터 타입 또는 방법 반환형) 로부터 추론되는 속성 : 예컨대 "mypackage.OrderAddress"의 경우 "orderAddress" 또는 "List<mypackage.OrderAddress>"의 경우 "orderAddressList"입니다.

이 요소의 값을 제공 했으므로 Spring MVC는 기본값을 사용합니다. 위에서 설명한 것처럼 기본값은 매개 변수의 유형 인 User에서 유추됩니다. 위의 예를 따르면 이 속성 이름은 user이고 User이 아니라는 것을 알 수 있습니다.이 또한 봄 참조 문서에 설명되어

${user.name} 

당신의 자리 읽어야

,

모델 속성 명에 때 어떤 일이 발생 here이 명시 적으로 지정되지 않는 이유는 무엇입니까? 그런 경우 해당 유형에 기반 모델 속성에 기본 이름이 지정됩니다. 예를 들어 메서드가 Account 유형의 개체를 반환하면 사용되는 기본 이름은 "account"입니다. 은 @ModelAttribute 주석의 값을 통해 변경할 수 있습니다. 속성을 Model에 직접 추가하는 경우 적절한 오버로드 된 addAttribute(..) 메소드를 사용하십시오 (예 : 속성 이름의 유무에 관계없이). 당신의 @GetMapping에서


, 당신은 명시 적으로 모델의 이름은

model.addAttribute("User2", new User()); 

당신은뿐만 아니라 당신의 @PostMapping 예를 들어 그렇게 할 수

@PostMapping("/login") 
public String greetingSubmit(@ModelAttribute(name = "User") User regUser) { 
속성 설정
관련 문제