参考资料
作用于Controller层中,在Controller层的方法执行前执行,主要作用是初始化当前Controller层的数据绑定器(或者属性绑定器),帮助完成数据处理和数据绑定。
被该注解修饰的方法会有一个形参WebDataBinder,可以帮我们将request请求中的参数处理绑定到JavaBean中。
import lombok.Data;import java.math.BigDecimal;
import java.util.Date;@Data
public class Test16Form {private String name;private String sex;private Date birthday;private BigDecimal money;
}
Title
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;@Controller
@RequestMapping("/test16")
public class Test16Controller {@InitBinderpublic void formBinder(WebDataBinder binder) {// 只要是String类型,就去除字符串前后的空格binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));// 只有当属性名为birthday且为Date类型的才使用使用框架自带的CustomDateEditor编辑器将String处理为DateDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");binder.registerCustomEditor(Date.class, "birthday", new CustomDateEditor(df, true));}@GetMapping("/init")public ModelAndView init() {ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("test16");return modelAndView;}@GetMapping("/receiveGet")@ResponseBodypublic void receiveGet(Test16Form form) {System.out.println(form);}
}
form对应的属性名[下标].实体类属性名
这种方式准备数据
Title
import lombok.Data;import java.math.BigDecimal;
import java.util.Date;
import java.util.List;@Data
public class Test16Form {private String name;private String sex;// 待转换类型private Date birthday;private BigDecimal money;private List tableList;
}
import lombok.Data;
import java.util.Date;@Data
public class Test4Entity {private String id;private String address;private String hobby;// 待转换类型private Date workDate;
}
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;@Controller
@RequestMapping("/test16")
public class Test16Controller {@InitBinderpublic void formBinder(WebDataBinder binder) {// 只要是String类型,就去除字符串前后的空格binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));// 自定义日期转换属性处理器binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {@Overridepublic void setAsText(String dateStr) {DateFormat dateFormat = null;try {if (ObjectUtils.isEmpty(dateStr)) {setValue(dateStr);return;}// yyyy-MM-dd HH:mm:ss格式if (dateStr.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");}// yyyy年MM月dd日 HH:mm:ss格式else if (dateStr.matches("^\\d{4}年\\d{1,2}月\\d{1,2}日 {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");}if (ObjectUtils.isEmpty(dateFormat)) {setValue(null);return;}Date parse = dateFormat.parse(dateStr);setValue(parse);} catch (Exception ex) {setValue(null);}}});}@GetMapping("/init")public ModelAndView init() {ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("test16");return modelAndView;}@PostMapping("/receivePost")@ResponseBodypublic void receivePost(Test16Form form) {System.out.println(form);}
}
import org.springframework.util.ObjectUtils;import java.beans.PropertyEditorSupport;
import java.util.Arrays;
import java.util.List;public class SexPropertyEditor extends PropertyEditorSupport {private final static List sexList = Arrays.asList("男", "女");@Overridepublic void setAsText(String sex) {// 当性别为空或者不是男或女的时候,默认设置为男性if(ObjectUtils.isEmpty(sex) || !sexList.contains(sex)) {setValue("男");return;}setValue(sex);}
}
import org.springframework.util.ObjectUtils;import java.beans.PropertyEditorSupport;public class StringToListPropertyEditor extends PropertyEditorSupport {@Overridepublic void setAsText(String text){if (ObjectUtils.isEmpty(text) || !text.contains("-")) {setValue(text);return;}setValue(text.split("-"));}
}
import lombok.Data;@Data
public class Test16Form01 {private String sex;private String[] numList;private String[] addList;
}
const url = `/test16/receiveNumListAndSex?sex=不明&numList=1-2-3&addList=4-5-6`;
$.ajax({url,type: 'GET',success: function (data, status, xhr) {console.log(data);}
});
@Controller
@RequestMapping("/test16")
public class Test16Controller {@InitBinderpublic void formBinder(WebDataBinder binder) {// 当数据类型为String[],且 属性名为 numList 的时候才会起作用// 虽然addList也是String[]格式的数据,但是我们并没有指定转换此属性binder.registerCustomEditor(String[].class, "numList", new StringToListPropertyEditor());// 当数据类型为String 且 属性名为 sex 的时候才会起作用binder.registerCustomEditor(String.class, "sex", new SexPropertyEditor());}@GetMapping("/receiveNumListAndSex")@ResponseBodypublic void receiveNumList(Test16Form01 form) {System.out.println(form);}
}
import com.example.jmw.common.bindEditor.SexPropertyEditor;
import com.example.jmw.common.bindEditor.StringToListPropertyEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;@Controller
@RequestMapping("/test16")
public class Test16Controller {// 注解没有添加value值,每个请求都会走此方法@InitBinderpublic void init(WebDataBinder binder) {binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));}// 指定只有参数名称为test16Form01的,才会走此方法@InitBinder("test16Form01")public void formBinder(WebDataBinder binder) {// 当数据类型为String 且 属性名为 sex 的时候才会起作用binder.registerCustomEditor(String.class, "sex", new SexPropertyEditor());}// 指定只有参数名称为test16Form的,才会走此方法@InitBinder("test16Form")public void receiveGetBinder(WebDataBinder binder) {// 当数据类型为String[],且 属性名为 numList 的时候才会起作用binder.registerCustomEditor(String[].class, "numList", new StringToListPropertyEditor());}@GetMapping("/receiveGet")@ResponseBodypublic void receiveGet(Test16Form form) {System.out.println(form);}@GetMapping("/receiveNumListAndSex")@ResponseBodypublic void receiveNumList(Test16Form01 form) {System.out.println(form);}
}