使用 thymeleaf 请求参数 [英] Request parameter with thymeleaf

查看:159
本文介绍了使用 thymeleaf 请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个 Spring Boot web 应用程序中,User 想要重置他的密码,所以他进入了 Reset password 页面.现在我想让他输入他的电子邮件地址,按 Reset,然后我想重定向到 myapp/resetPassword?email=HIS_EMAIL 来处理密码重置.

我的代码:

@RequestMapping(value = "/resetPassword", method = RequestMethod.GET)public String resetPasswordForm(模型模型){model.addAttribute("email", new String());返回重置密码";}@RequestMapping(value = "/resetPassword", method = RequestMethod.POST)public String resetPassword(@RequestParam("email") String email) {用户用户 = userService.findUserByEmail(email);//玩逻辑返回重定向:/";}

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

<input type="email" th:field="${email}" th:placeholder="Email"/><div class="clearfix"><button type="submit">重置</button>

</表单>

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

解决方案

th:field"仅适用于实体 Bean.这应该有效:

@GetMapping(value = "/resetPassword")public String resetPassword(@RequestParam(value=email",required=false) String email) {如果(电子邮件==空)返回重置密码";用户用户 = userService.findUserByEmail(email);//玩逻辑返回重定向:/";}<form th:action="@{/resetPassword}";方法=获取"><输入类型=电子邮件"th:name="电子邮件"th:placeholder="电子邮件"/><div class="clearfix"><button type="submit">Reset</button>

</表单>

不要忘记:Thymeleaf 不是 Javascript.它在服务器上呈现.@{/resetPassword(email=${email})} 之类的东西会输出例如/resetPassword?email=anValueYouAddedToModelInController

In a Spring Boot web app, User wants to reset his password, so he enters Reset password page. Now I want to let him type his email address, push Reset and I want to redirect to myapp/resetPassword?email=HIS_EMAIL to handle password reset.

MY code:

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

How can I achieve it on my thymeleaf page? I tried something like this:

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

Unfortunately my email is always null. Can somebody help?

解决方案

"th:field" is for Entity-Beans only. This should work:

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

And don't forget: Thymeleaf is not Javascript. It is rendered on Server. Things like @{/resetPassword(email=${email})} would output e.g. /resetPassword?email=anValueYouAddedToModelInController

这篇关于使用 thymeleaf 请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆