Spring Boot 表单;隐藏路径值并显示占位符

Posted

技术标签:

【中文标题】Spring Boot 表单;隐藏路径值并显示占位符【英文标题】:Spring Boot Forms; hiding the path value and showing placeholder 【发布时间】:2021-01-28 09:22:39 【问题描述】:

当您到达 editProfile.jsp 时,我在 Spring Boot 表单显示路径值而不是占位符的信息时遇到了一个小问题。我希望输入字段看起来像这样; Edit Profile Page 而不是这个Wrong Edit Profile。我不希望我的用户必须单击、选择和删除自动完成的值。我希望它只显示占位符并允许他们轻松覆盖显示的内容。

这是editProfile.jsp

<%--@elvariable id="editProfile" type=""--%>
<form:form method="POST" modelAttribute="editProfile">
    <div class="MyForm form-group">
        <h1>Edit Profile</h1>
        <form:input type="email" class="MyInput" id="email" path="email" placeholder="$editProfile.email" />

         <form:button type="submit" class="from-control">Submit</form:button>


    </div>

    <div>
        <img src="images/reg1.png" >
    </div>



</form:form>
</body>
</html>

这是控制器中指定的代码

@RequestMapping(value = "edit/email", method = RequestMethod.GET)
    public String getEditUserData(@PathVariable("email") String email, Model model) 

        AccountEntity accountInstance = accountRepo.findByEmail(email);
        model.addAttribute("editProfile", accountInstance);

        return "editProfile";
    

    @RequestMapping(value = "edit/email", method = RequestMethod.POST)
    public String enterEditUserData(@ModelAttribute("login") AccountEntity accountForm, @PathVariable("email") String email, Model model ) 
        AccountEntity accountInstance = accountRepo.findByEmail(email);
        accountInstance.setEmail(accountForm.getEmail());
        accountRepo.save(accountInstance);

        return "redirect:/login";
    

【问题讨论】:

【参考方案1】:

我想通了;您必须添加一个新Entity的模型,因此路径变量不会填充特定路径值的实例。这是新代码,并与我上面发送的代码进行比较。

@RequestMapping(value = "edit/email", method = RequestMethod.GET)
    public String getEditUserData(@PathVariable("email") String email, Model model) 

        AccountEntity accountInstance = accountRepo.findByEmail(email);
        model.addAttribute("editProfile2", new AccountEntity());
        model.addAttribute("editProfile1", accountInstance);

        return "editProfile";
    
<%--@elvariable id="editProfile" type=""--%>

<%--@elvariable id="editProfile2" type=""--%>
<form:form method="POST" modelAttribute="editProfile2">
    <div class="grid form-group">
        <h1>Edit Profile</h1>

        <form:input type="email" class="MyInput" id="email" path="email" placeholder='$editProfile1.email' />


         <form:button type="submit" class="from-control">Submit</form:button>


    </div>

【讨论】:

以上是关于Spring Boot 表单;隐藏路径值并显示占位符的主要内容,如果未能解决你的问题,请参考以下文章

重置 select2 值并显示占位符

更改隐藏值并使用 onclick for href 提交表单

Spring-Boot Thymeleaf 绑定枚举在隐藏字段

spring-boot 属性占位符

php 使用占位符时隐藏重力表单字段标签

深入Spring Boot:那些注入不了的Spring占位符(${}表达式)