Using the setAllowedFields() method in Spring

后端 未结 4 717
后悔当初
后悔当初 2021-01-15 05:56

I\'m using Spring 3.2.0. I have registered a few custom property editors for some basic needs as follows.

import editors.DateTimeEditor;
import edito         


        
4条回答
  •  逝去的感伤
    2021-01-15 06:08

    A solution to use binder with DTO (companydata in example) in case most of the form input values should be converted to null if empty, but there is a need to add few exceptions (setDisallowedFields didn't work for me).

    @InitBinder()
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
    
    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        binder.registerCustomEditor(String.class, "companydata.companyName", new StringTrimmerEditor(false));
        binder.registerCustomEditor(String.class, "companydata.companyNumber", new StringTrimmerEditor(false));
    }
    

提交回复
热议问题