Thymeleaf-如果存在值,如何填充输入字段?

时间:2019-04-22 13:30:15

标签: spring-boot thymeleaf

我有一个Spring Boot应用程序,其流程如下。

  • 新用户注册已注册
  • 他/她收到验证邮件
  • 当用户单击验证URL时,他/她将被重定向到登录页面。

到目前为止,一切都很好,但是我想做的是当用户进入登录页面时,我想将用户的电子邮件填充到电子邮件字段中,并让用户输入他/她的密码以继续。

我的电子邮件字段如下:

def index
  if params[:search].present?
    @topics = Topic.search(params[:search]).paginate(:page => params[:page], :per_page => 5)
    flash[:notice] = "No records found based on the search." if @topics.blank?
  else
    @topics = Topic.all.paginate(:page => params[:page], :per_page => 5) #add this
    flash[:notice] = "No records found in Database." if @topics.blank?
  end
end

当用户重定向到登录页面时,我将用户的电子邮件添加到模型属性中,如下所示:

<input type="email"
    id="userLoginFormEmail"
    class="form-control mb-4"
    th:field="*{email}"
    placeholder="Your email" required />

那么,如何预填充电子邮件字段为model.addAttribute("userEmail", userEmail); return "user/login"; 属性不为空?

更新

我在登录页面上有一个警报,当访问激活URL时,该警报看起来很好。看起来像这样

userEmail

因此,<div class="alert alert-info mt-5" role="alert" th:if="${userEmail != null}"> <strong>Your account is activated. Please login to proceed.</strong> <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> 可以正常工作,但仍无法在电子邮件字段中显示它。

2 个答案:

答案 0 :(得分:1)

在控制器中,在userEmail对象上设置用户电子邮件(在呈现页面之前)。

答案 1 :(得分:0)

将属性th:value="userEmail"添加到模板的输入字段中