Trim input values in Thymeleaf using Spring MVC

2019-09-08 01:17发布

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!

1条回答
放我归山
2楼-- · 2019-09-08 01:46

My Solution

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("( )+", " ");
    }
}
查看更多
登录 后发表回答