【问题标题】:Spring boot thymeleaf field passing form inputs between pagesSpring Boot thymeleaf 字段在页面之间传递表单输入
【发布时间】:2017-08-08 10:30:02
【问题描述】:

我有一个 HTML 表单,允许用户为基本搜索功能输入用户名:

<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 页面中创建的 th:field:

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

第一个 HTML 页面加载正常,但是当我按下提交时,我收到 EL1007E 的错误消息:在 null 上找不到属性或字段 'usern'。

这是我的模型:

public class Searchuser {

    private String usern;

    public String getUsern() {
        return usern;
    }

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

这是我的控制器:

@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";
    }
}

【问题讨论】:

    标签: java html spring-boot thymeleaf


    【解决方案1】:

    替换

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

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

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 2017-12-10
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 2020-06-20
      • 2019-10-08
      相关资源
      最近更新 更多