gpt4 book ai didi

spring - 使用 thymeleaf 请求参数

转载 作者:行者123 更新时间:2023-12-02 07:03:44 24 4
gpt4 key购买 nike

在 Spring Boot Web 应用程序中,用户想要重置密码,因此他进入重置密码页面。现在我想让他输入他的电子邮件地址,按 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:/";
}

如何在我的 thymeleaf 页面上实现它?我尝试过这样的事情:

<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>

不幸的是,我的电子邮件始终为空。有人可以帮忙吗?

最佳答案

“th:field”仅适用于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

关于spring - 使用 thymeleaf 请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43305607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com