Can spring mvc trim all strings obtained from forms?

前端 未结 6 1651

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:22

    first,trim requestparam which is String,you can create a class and implimplements WebBingdingInitializer

     @ControllerAdvice
    public class CustomWebBindingInitializer implements WebBindingInitializer {
    
        @InitBinder
        @Override
        public void initBinder(WebDataBinder webDataBinder, WebRequest webRequest) {
            webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        }
    }
    

    please use componentScan make this Class to be a Spring Bean.

    But, I don't know how to trim the String value in requestBody JSON data.

提交回复
热议问题