Java Code Examples for org.springframework.web.bind.annotation.InitBinder

The following examples show how to use org.springframework.web.bind.annotation.InitBinder. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@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);
	});
}
 
Example 2
Source Project: SA47   Source File: StaffController.java    License: The Unlicense 5 votes vote down vote up
@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);

}
 
Example 3
/**
 * 局部日期转换,将 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));
   }
 
Example 4
@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));
}
 
Example 5
@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));
}
 
Example 6
@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));
}
 
Example 7
@InitBinder
protected void initBinder( WebDataBinder binder )
{
    binder.registerCustomEditor( Date.class, new PropertyEditorSupport()
    {
        @Override
        public void setAsText( String value ) throws IllegalArgumentException
        {
            setValue( DateUtils.parseDate( value ) );
        }
    } );
}
 
Example 8
Source Project: pcf-crash-course-with-spring-boot   Source File: TodoController.java    License: MIT License 5 votes vote down vote up
@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));
}
 
Example 9
@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));
}
 
Example 10
Source Project: lams   Source File: HandlerMethodInvoker.java    License: GNU General Public License v2.0 5 votes vote down vote up
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);
					}
				}
			}
		}
	}
}
 
Example 11
Source Project: springlets   Source File: ValidatorAdvice.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
  }
}
 
Example 12
Source Project: DimpleBlog   Source File: BaseController.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 将前台传递过来的日期格式的字符串,自动转化为Date类型
 */
@InitBinder
public void initBinder(WebDataBinder binder) {
    // Date 类型转换
    binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            setValue(DateUtils.parseDate(text));
        }
    });
}
 
Example 13
@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));
}
 
Example 14
@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));
}
 
Example 15
Source Project: jeewx   Source File: BaseController.java    License: Apache License 2.0 5 votes vote down vote up
/**
	 * 将前台传递过来的日期格式的字符串,自动转化为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());
	}
 
Example 16
@Override
@InitBinder
public void initBinder(@RequestParam("param1") String p1,
		@RequestParam(value="paramX", required=false) String px, int param2) {

	assertNull(px);
}
 
Example 17
@InitBinder
public void initBinder(WebDataBinder binder) {
	binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
}
 
Example 18
@InitBinder
protected void initBinder(WebDataBinder binder, HttpServletRequest request) {
    binder.addValidators(new UserSettingsValidator(securityService, settingsService, request));
}
 
Example 19
Source Project: zuihou-admin-cloud   Source File: IndexController.java    License: Apache License 2.0 4 votes vote down vote up
@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));
}
 
Example 20
Source Project: spring-analysis-note   Source File: ModelInitializerTests.java    License: MIT License 4 votes vote down vote up
@InitBinder
public void initDataBinder(WebDataBinder dataBinder) {
	if (this.validator != null) {
		dataBinder.addValidators(this.validator);
	}
}
 
Example 21
@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));
}
 
Example 22
@InitBinder
public void initBinderTypeConversion(WebDataBinder dataBinder, @RequestParam int requestParam) {
	dataBinder.setDisallowedFields("requestParam-" + requestParam);
}
 
Example 23
protected boolean isInitBinderMethod(Method method) {
	return AnnotationUtils.findAnnotation(method, InitBinder.class) != null;
}
 
Example 24
Source Project: onboard   Source File: NewInvitationController.java    License: Apache License 2.0 4 votes vote down vote up
@InitBinder
protected void initRegistrationBinder(WebDataBinder binder) {
    binder.setValidator(validator);
}
 
Example 25
@InitBinder
void initDataBinder() {}
 
Example 26
@InitBinder("dateParam")
public void initBinder(WebDataBinder dataBinder, @RequestParam("datePattern") String datePattern) {
	SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
	dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
 
Example 27
@InitBinder //④
public void initBinder(WebDataBinder webDataBinder) {
	webDataBinder.setDisallowedFields("id"); //⑤
}
 
Example 28
Source Project: egeria   Source File: OpenLineageController.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 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());
}
 
Example 29
Source Project: DevOps-for-Web-Development   Source File: VisitController.java    License: MIT License 4 votes vote down vote up
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
    dataBinder.setDisallowedFields("id");
}
 
Example 30
Source Project: maven-framework-project   Source File: ProductController.java    License: MIT License 4 votes vote down vote up
@InitBinder
public void initialiseBinder(WebDataBinder binder) {
	binder.setAllowedFields("productId","name","unitPrice","description","manufacturer","category","unitsInStock", "condition","productImage");
}