使用 Spring MVC 在 Thymeleaf 中修剪输入值 [英] Trim input values in Thymeleaf using Spring MVC

查看:39
本文介绍了使用 Spring MVC 在 Thymeleaf 中修剪输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在使用 Thymeleaf 将输入值发送到控制器之前修剪?

我使用 Spring MVC 和 Thymeleaf 作为模板引擎.

请看我的表单代码:

<form id="collabForm" method="post" action="#" th:action="@{/collaborateurs/add}" role="form" th:object="${newCollaborateur}"><div class="box-body"><div class="form-group col-sm-6 col-md-4 col-lg-4"><label for="nomCollab">Nom</label><input id="nomCollab" class="form-control" name="nom" type="text" placeholder="Saisir le nom" th:field="*{nom}" required="required"/>

</表单>

非常感谢!

解决方案

您可以使用 Spring StringTrimmerEditor:

public abstract class BaseController//你的超级控制器类@InitBinder公共无效initBinder(最终WebDataBinder绑定器){最终 StringTrimmerEditor stringtrimmer =新的 StringTrimmerEditor(true);binder.registerCustomEditor(String.class, stringtrimmer);}}

I would like to know how can I trim an input value before send it to the controller with Thymeleaf?

I'm using Spring MVC and Thymeleaf as a template engine.

Bellow the code of my form:

<form id="collabForm" method="post" action="#" th:action="@{/collaborateurs/add}" role="form" th:object="${newCollaborateur}">
  <div class="box-body">
    <div class="form-group col-sm-6 col-md-4 col-lg-4">
      <label for="nomCollab">Nom</label>
      <input id="nomCollab" class="form-control" name="nom" type="text" placeholder="Saisir le nom" th:field="*{nom}" required="required"/>
    </div>
  </div>
</form>  

Thanks a lot!

解决方案

You can use the Spring StringTrimmerEditor:

public abstract class BaseController // your super controller class

    @InitBinder
    public void initBinder(final WebDataBinder binder) {

        final StringTrimmerEditor stringtrimmer = 
            new StringTrimmerEditor(true);
        binder.registerCustomEditor(String.class, stringtrimmer);
    }
}

这篇关于使用 Spring MVC 在 Thymeleaf 中修剪输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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