下面列出了怎么用org.springframework.web.bind.annotation.InitBinder的API类实例代码及写法,或者点击链接到github查看源代码。
@InitBinder
public void initBinder(WebDataBinder binder,
@RequestParam("date-pattern") Optional<String> optionalPattern) {
optionalPattern.ifPresent(pattern -> {
CustomDateEditor dateEditor = new CustomDateEditor(new SimpleDateFormat(pattern), false);
binder.registerCustomEditor(Date.class, dateEditor);
});
}
@InitBinder("course")
private void initCourseBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
binder.addValidators(cValidator);
}
/**
* 局部日期转换,将 String 类型的时间数据转化为 Date 类型
* @param binder
* @param request
*/
@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
// 转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
// CustomDateEditor为自定义日期编辑器
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.initBeanPropertyAccess();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
@InitBinder({"myCommand", "date"})
public void initBinder(WebDataBinder binder, String date, @RequestParam("date") String[] date2) {
LocalValidatorFactoryBean vf = new LocalValidatorFactoryBean();
vf.afterPropertiesSet();
binder.setValidator(vf);
assertEquals("2007-10-02", date);
assertEquals(1, date2.length);
assertEquals("2007-10-02", date2[0]);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
@SuppressWarnings("unused")
@InitBinder({"myCommand", "date"})
private void initBinder(WebDataBinder binder, String date, @RequestParam("date") String[] date2) {
LocalValidatorFactoryBean vf = new LocalValidatorFactoryBean();
vf.afterPropertiesSet();
binder.setValidator(vf);
assertEquals("2007-10-02", date);
assertEquals(1, date2.length);
assertEquals("2007-10-02", date2[0]);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
@InitBinder
protected void initBinder( WebDataBinder binder )
{
binder.registerCustomEditor( Date.class, new PropertyEditorSupport()
{
@Override
public void setAsText( String value ) throws IllegalArgumentException
{
setValue( DateUtils.parseDate( value ) );
}
} );
}
@InitBinder
public void initBinder(WebDataBinder binder) {
// Date - dd/MM/yyyy
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));
}
@SuppressWarnings("unused")
@InitBinder
private void initBinder(WebDataBinder binder) {
binder.initBeanPropertyAccess();
binder.setRequiredFields("sex");
LocalValidatorFactoryBean vf = new LocalValidatorFactoryBean();
vf.afterPropertiesSet();
binder.setValidator(vf);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
protected void initBinder(Object handler, String attrName, WebDataBinder binder, NativeWebRequest webRequest)
throws Exception {
if (this.bindingInitializer != null) {
this.bindingInitializer.initBinder(binder, webRequest);
}
if (handler != null) {
Set<Method> initBinderMethods = this.methodResolver.getInitBinderMethods();
if (!initBinderMethods.isEmpty()) {
boolean debug = logger.isDebugEnabled();
for (Method initBinderMethod : initBinderMethods) {
Method methodToInvoke = BridgeMethodResolver.findBridgedMethod(initBinderMethod);
String[] targetNames = AnnotationUtils.findAnnotation(initBinderMethod, InitBinder.class).value();
if (targetNames.length == 0 || Arrays.asList(targetNames).contains(attrName)) {
Object[] initBinderArgs =
resolveInitBinderArguments(handler, methodToInvoke, binder, webRequest);
if (debug) {
logger.debug("Invoking init-binder method: " + methodToInvoke);
}
ReflectionUtils.makeAccessible(methodToInvoke);
Object returnValue = methodToInvoke.invoke(handler, initBinderArgs);
if (returnValue != null) {
throw new IllegalStateException(
"InitBinder methods must not have a return value: " + methodToInvoke);
}
}
}
}
}
}
/**
* Adds the {@link CollectionValidator} to the supplied {@link WebDataBinder}
*
* @param binder web data binder.
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
Object target = binder.getTarget();
if (target != null && collectionValidator.supports(target.getClass())) {
binder.addValidators(collectionValidator);
}
}
/**
* 将前台传递过来的日期格式的字符串,自动转化为Date类型
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
// Date 类型转换
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(DateUtils.parseDate(text));
}
});
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.initBeanPropertyAccess();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
@InitBinder
public void initBinder(WebDataBinder binder, @PathVariable("hotel") String hotel) {
assertEquals("Invalid path variable value", "42", hotel);
binder.initBeanPropertyAccess();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
/**
* 将前台传递过来的日期格式的字符串,自动转化为Date类型
*
* @param binder
*/
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
// SimpleDateFormat dateFormat = new SimpleDateFormat(
// "yyyy-MM-dd hh:mm:ss");
// binder.registerCustomEditor(Date.class, new CustomDateEditor(
// dateFormat, true));
binder.registerCustomEditor(Date.class, new DateConvertEditor());
}
@Override
@InitBinder
public void initBinder(@RequestParam("param1") String p1,
@RequestParam(value="paramX", required=false) String px, int param2) {
assertNull(px);
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
}
@InitBinder
protected void initBinder(WebDataBinder binder, HttpServletRequest request) {
binder.addValidators(new UserSettingsValidator(securityService, settingsService, request));
}
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
@InitBinder
public void initDataBinder(WebDataBinder dataBinder) {
if (this.validator != null) {
dataBinder.addValidators(this.validator);
}
}
@InitBinder({"myCommand", "date"})
private void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
@InitBinder
public void initBinderTypeConversion(WebDataBinder dataBinder, @RequestParam int requestParam) {
dataBinder.setDisallowedFields("requestParam-" + requestParam);
}
protected boolean isInitBinderMethod(Method method) {
return AnnotationUtils.findAnnotation(method, InitBinder.class) != null;
}
@InitBinder
protected void initRegistrationBinder(WebDataBinder binder) {
binder.setValidator(validator);
}
@InitBinder
void initDataBinder() {}
@InitBinder("dateParam")
public void initBinder(WebDataBinder dataBinder, @RequestParam("datePattern") String datePattern) {
SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
@InitBinder //④
public void initBinder(WebDataBinder webDataBinder) {
webDataBinder.setDisallowedFields("id"); //⑤
}
/**
* This method is registering a custom converter for View and Scope enums in order to be able to use in url the text of the enum and not the actual name
* @param webdataBinder DataBinder for data binding from web request parameters to JavaBean objects
*/
@InitBinder
public void initBinder(final WebDataBinder webdataBinder) {
webdataBinder.registerCustomEditor(Scope.class, new ScopeEnumConverter());
}
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@InitBinder
public void initialiseBinder(WebDataBinder binder) {
binder.setAllowedFields("productId","name","unitPrice","description","manufacturer","category","unitsInStock", "condition","productImage");
}
如果文章对你有帮助,欢迎点击上方按钮打赏作者