Thymeleaf th:href获取其他输入字段值?

时间:2017-01-16 17:37:18

标签: spring-boot thymeleaf

这是我的表单,输入字段电子邮件&密码

<form class="form-signin" th:action="@{/login}" method="post">
    <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required="required" autofocus="autofocus" /> 
    <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required="required" />
    <button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Sign in</button>
</form>

我想尝试使用Thymeleaf th:href发送带参数的请求

这是我的代码:

<a href="#" th:href="@{/user/forgot_password{email}(email=$(inputEmail).val())}" class="forgot-password"> Forgot the password? </a>

我的Spring-Boot控制器如下:

@RequestMapping(value = "/forgot_password", params = {"email!="}, method = RequestMethod.GET)
public @ResponseBody RespCommon forgotPassword(
    @RequestParam(value = "email", required = true) String email) {
    userService.forgotPassword(email);
    return new RespCommon(ResultCode.SUCCESS, "Send forget password mail succeed");
}

但它似乎无法正常工作,有些人可以分享如何获得其他输入字段值,非常感谢!

1 个答案:

答案 0 :(得分:0)

使用链接(因此使用GET方法)来恢复密码对我来说不是一个好主意。例如,浏览器或电子邮件客户端可能会遵循它。

相反,我建议使用看起来像链接的表单(使用POST方法)。您可以将电子邮件放在hidden字段中,并将btn btn-link类添加到提交按钮。要填充电子邮件值,您可以使用Model.addAttribute()从控制器导出值。