请求参数与百里香

时间:2017-04-09 10:21:33

标签: spring thymeleaf

在Spring Boot网络应用中,User想要重置密码,因此他会进入Reset password页面。现在我想让他输入他的电子邮件地址,按Reset我想重定向到myapp/resetPassword?email=HIS_EMAIL以处理密码重置。

我的代码:

@RequestMapping(value = "/resetPassword", method = RequestMethod.GET)
public String resetPasswordForm(Model model){
    model.addAttribute("email", new String());
    return "resetPassword";
}

@RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
public String resetPassword(@RequestParam("email") String email) {
    User user = userService.findUserByEmail(email);
    //playing with logic
    return "redirect:/";
}

如何在我的百万美元页面上实现它?我试过这样的事情:

<form th:action="@{/resetPassword(email=${email})}" method="post">
    <input type="email" th:field="${email}" th:placeholder="Email" />
        <div class="clearfix">
            <button type="submit">Reset</button>
        </div>
</form>

不幸的是我的email始终为空。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:5)

&#34;第:字段&#34;仅适用于Entity-Beans。这应该有效:

@GetMapping(value = "/resetPassword")
public String resetPassword(@RequestParam(value="email",required=false) String email) {
    if(email==null)
        return "resetPassword";
    User user = userService.findUserByEmail(email);
    //playing with logic
    return "redirect:/";     
}

<form th:action="@{/resetPassword}" method="get">
    <input type="email" th:name="email" th:placeholder="Email" />
    <div class="clearfix">
        <button type="submit">Reset</button>
    </div>
</form>

不要忘记:Thymeleaf不是Javascript。它在服务器上呈现。认为@{/resetPassword(email=${email})}会输出例如/resetPassword?email=anValueYouAddedToModelInController