Can spring mvc trim all strings obtained from forms?

前端 未结 6 1652

I know struts2 default config will trim all strings obtained from forms.

For example:

I type

\"   whatever \"
in a form and submit, I will get

6条回答
  •  盖世英雄少女心
    2020-12-13 03:10

    Just customized the above code in order to adjust to Spring Boot, if you want to explicit trim function for some fields in the form, you can show them as below:

    @Component
    @ControllerAdvice
    public class ControllerSetup {
        @InitBinder({"dto", "newUser"})
            public void initBinder(WebDataBinder binder) {
              binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
              binder.registerCustomEditor(String.class, "userDto.username", new StringTrimmerEditor(false));
              binder.registerCustomEditor(String.class, "userDto.password", new DefaultStringEditor(false));
              binder.registerCustomEditor(String.class, "passwordConfirm", new DefaultStringEditor(false));
            }
    }
    

提交回复
热议问题