2017-09-22 93 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是:

<!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>Result</h1> 
    <p th:text="'username: ' + ${User.name}" /> 
    <p th:text="'password: ' + ${regUser != null ? regUser.password : 'NOT FOUND'}" /> 
    <p th:text="'balance: ' + ${model != null ? regUser.balance : 'NOT FOUND'}" /> 
    <a href="/demo/login">Submit another user?</a> 
</body> 
</html> 

该公司密切关注该行多次抛出一个空的错误:

<p th:text="'username: ' + ${User.name}" /> 

特别${User.name},看起来像

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null

所以我用尽了一切我能想到的的,

${User2.name} 
${User} 
${User2} 
${regUser.name} 
${teststr} 
${#User.name} 

等....

这是很奇怪的,如果我取消System.out.println(regUser.getName()); 这将打印用户的名字就好了,但不会传递变量。它是我的占位符还是不能作为th:text传递,因为它是User对象?请帮助我很困惑!

编辑:教程我遵循(https://spring.io/guides/gs/handling-form-submission/) 如果您复制教程它工作得很好,但它只是传递一个字符串。虽然User.name将是一个字符串变量...

回答

0

@ModelAttribute有一个名为name的元素。它的javadoc状态

要绑定到的模型属性的名称。

默认模型属性名称是从声明的 属性类型(即方法参数类型或方法返回类型) 基于非限定类名称推断的。 "orderAddress"等级 "mypackage.OrderAddress""orderAddressList"对于 "List<mypackage.OrderAddress>"

由于您已经为此元素提供了值,Spring MVC使用默认值。如上所述,默认值是根据您的参数的类型推断的,即User。如果您按照示例,那个属性名称将因此是user,而不是User

你的占位符应阅读

${user.name} 

这是Spring参考文档中还解释,here

当一个模型属性的名称会发生​​什么事是没有明确规定? 在这种情况下,默认名称将根据类型分配给基于 的模型属性。例如,如果方法返回类型为 Account的对象,则使用的默认名称是"account"。您可以通过@ModelAttribute注释的值更改 。如果将 属性直接添加到Model,请使用适当的过载 addAttribute(..)方法 - 即使用或不使用属性名称。


在你@GetMapping,明确设置模型的name属性

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

你可以做你的@PostMapping例如以及

@PostMapping("/login") 
public String greetingSubmit(@ModelAttribute(name = "User") User regUser) {