【问题标题】:Trim input values in Thymeleaf using Spring MVC使用 Spring MVC 修剪 Thymeleaf 中的输入值
【发布时间】:2017-03-18 11:49:33
【问题描述】:

我想知道如何在使用 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"/>
    </div>
  </div>
</form>  

非常感谢!

【问题讨论】:

  • 你的输入标签通常使用onchange="this.value = this.value.trim()"怎么样?
  • 感谢您的回答,但我认为有一种简单的方法可以做到这一点。我不知道怎么做,但似乎 Thymeleaf 对此有所帮助

标签: html spring-mvc thymeleaf


【解决方案1】:

我的解决方案

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.io.IOException;

@ControllerAdvice
public class StringTrimModule extends PropertyEditorSupport {

    @Component
    public class JsonStringTrimModule extends SimpleModule {
        public JsonStringTrimModule() {
            addDeserializer(String.class, new StdDeserializer<String>(String.class) {
                @Override
                public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
                    switch (jsonParser.getCurrentName()) {
                        case "password":
                            return _parseString(jsonParser, context);
                        default:
                            return trim(_parseString(jsonParser, context));
                    }
                }
            });

            addSerializer(String.class, new StdSerializer<String>(String.class) {
                @Override
                public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
                    gen.writeString(trim(value));
                }
            });
        }
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields("*password");
        binder.registerCustomEditor(String.class, this);
    }

    @Override
    public void setAsText(@Nullable String text) {
        if (text == null) {
            setValue(null);
        } else {
            String value = trim(text);
            if ("".equals(value)) {
                setValue(null);
            } else {
                setValue(value);
            }
        }
    }

    private String trim(String value) {
        return value.trim().replaceAll("( )+", " ");
    }
}

【讨论】:

    【解决方案2】:

    您可以使用 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2016-02-16
      • 2023-03-13
      • 2018-01-07
      • 2018-06-12
      • 2019-06-28
      相关资源
      最近更新 更多