2017-08-08 222 views
0

我有一個HTML表單,允許用戶輸入用戶名的一個基本的搜索功能之間的場傳遞表單輸入:春天開機thymeleaf頁

<form action="#" th:action="@{/admin}" th:object="${searchuser}" method="post"> 
     <p>Username: <input type="text" th:field="*{usern}" /></p> 
     <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> 
    </form> 

結果則意味着在不同的HTML來顯示頁面,在這裏我打電話的次:在第一個HTML頁面中創建領域:

<div class="formcontainer"> 
     <h1>Result</h1> 
     <p th:text="${UserSearch.usern}" /> 
     <a href="/admin">Submit another message</a> 
    </div> 

第一個HTML頁面加載罰款,但是當我按下提交我得到EL1007E的錯誤消息:屬性或字段「用戶N」不能在null上找到。

這裏是我的模型:

public class Searchuser { 
    private String usern; 

    public String getUsern() { 
    return usern; 
    } 

    public void setUsern(String usern) { 
    this.usern = usern; 
    } 
} 

這裏是我的控制器:

package com.compman.controller; 

    import org.springframework.stereotype.Controller; 
    import org.springframework.ui.Model; 
    import org.springframework.web.bind.annotation.GetMapping; 
    import org.springframework.web.bind.annotation.ModelAttribute; 
    import org.springframework.web.bind.annotation.PostMapping; 

    import com.compman.models.Searchuser; 

    @Controller 
    public class UserController { 

    @GetMapping("/admin") 
    public String greetingForm(Model model) { 
     model.addAttribute("searchuser", new Searchuser()); 
     return "admin"; 
    } 

    @PostMapping("/admin") 
    public String greetingSubmit(@ModelAttribute Searchuser searchuser) { 
     return "userresults"; 
    } 
} 

回答

0

替代

<p th:text="${UserSearch.usern}" /> 

<p th:text="${searchuser.usern}" />