org.springframework.beans.propertyeditors.CustomDateEditor Java Examples

The following examples show how to use org.springframework.beans.propertyeditors.CustomDateEditor. 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
Source Project: spring-analysis-note   Author: Vip-Augus   File: BindTagTests.java    License: MIT License 6 votes vote down vote up
@Test
public void transformTagNonExistingValue() throws JspException {
	// first set up the pagecontext and the bean
	PageContext pc = createPageContext();
	TestBean tb = new TestBean();
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
	CustomDateEditor l = new CustomDateEditor(df, true);
	binder.registerCustomEditor(Date.class, l);
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult());

	// try with non-existing value
	BindTag bind = new BindTag();
	bind.setPageContext(pc);
	bind.setPath("tb.name");
	bind.doStartTag();

	TransformTag transform = new TransformTag();
	transform.setPageContext(pc);
	transform.setValue(null);
	transform.setParent(bind);
	transform.setVar("theString2");
	transform.doStartTag();

	assertNull(pc.getAttribute("theString2"));
}
 
Example #2
Source Project: java-technology-stack   Author: codeEngraver   File: BindTagTests.java    License: MIT License 6 votes vote down vote up
@Test
public void transformTagNonExistingValue() throws JspException {
	// first set up the pagecontext and the bean
	PageContext pc = createPageContext();
	TestBean tb = new TestBean();
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
	CustomDateEditor l = new CustomDateEditor(df, true);
	binder.registerCustomEditor(Date.class, l);
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult());

	// try with non-existing value
	BindTag bind = new BindTag();
	bind.setPageContext(pc);
	bind.setPath("tb.name");
	bind.doStartTag();

	TransformTag transform = new TransformTag();
	transform.setPageContext(pc);
	transform.setValue(null);
	transform.setParent(bind);
	transform.setVar("theString2");
	transform.doStartTag();

	assertNull(pc.getAttribute("theString2"));
}
 
Example #3
Source Project: spring-analysis-note   Author: Vip-Augus   File: RequestMappingDataBindingIntegrationTests.java    License: MIT License 5 votes vote down vote up
@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 #4
@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 #5
Source Project: spring-analysis-note   Author: Vip-Augus   File: ServletAnnotationControllerHandlerMethodTests.java    License: MIT License 5 votes vote down vote up
@InitBinder
public 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 #6
Source Project: spring-analysis-note   Author: Vip-Augus   File: ServletAnnotationControllerHandlerMethodTests.java    License: MIT License 5 votes vote down vote up
@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 #7
Source Project: spring-analysis-note   Author: Vip-Augus   File: ServletAnnotationControllerHandlerMethodTests.java    License: MIT License 5 votes vote down vote up
@Override
public void initBinder(WebDataBinder binder) {
	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 #8
Source Project: spring-analysis-note   Author: Vip-Augus   File: ServletAnnotationControllerHandlerMethodTests.java    License: MIT License 5 votes vote down vote up
@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 #9
Source Project: spring-analysis-note   Author: Vip-Augus   File: BindTagTests.java    License: MIT License 5 votes vote down vote up
@Test
public void transformTagWithHtmlEscape() throws JspException {
	// first set up the PageContext and the bean
	PageContext pc = createPageContext();
	TestBean tb = new TestBean();
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
	CustomDateEditor l = new CustomDateEditor(df, true);
	binder.registerCustomEditor(Date.class, l);
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult());

	// try another time, this time using Strings
	BindTag bind = new BindTag();
	bind.setPageContext(pc);
	bind.setPath("tb.name");
	bind.doStartTag();

	TransformTag transform = new TransformTag();
	transform.setPageContext(pc);
	transform.setValue("na<me");
	transform.setParent(bind);
	transform.setVar("theString");
	transform.setHtmlEscape(true);
	transform.doStartTag();

	assertNotNull(pc.getAttribute("theString"));
	assertEquals("na&lt;me", pc.getAttribute("theString"));
}
 
Example #10
Source Project: spring-analysis-note   Author: Vip-Augus   File: ConcurrentBeanFactoryTests.java    License: MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	Assume.group(TestGroup.PERFORMANCE);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
			qualifiedResource(ConcurrentBeanFactoryTests.class, "context.xml"));

	factory.addPropertyEditorRegistrar(
			registry -> registry.registerCustomEditor(Date.class,
					new CustomDateEditor((DateFormat) DATE_FORMAT.clone(), false)));

	this.factory = factory;
}
 
Example #11
Source Project: pcf-crash-course-with-spring-boot   Author: in28minutes   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 #12
Source Project: pcf-crash-course-with-spring-boot   Author: in28minutes   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 #13
Source Project: kubernetes-crash-course   Author: in28minutes   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 #14
Source Project: kubernetes-crash-course   Author: in28minutes   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 #15
Source Project: docker-crash-course   Author: in28minutes   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 #16
Source Project: docker-crash-course   Author: in28minutes   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 #17
Source Project: java-technology-stack   Author: codeEngraver   File: RequestMappingDataBindingIntegrationTests.java    License: MIT License 5 votes vote down vote up
@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 #18
@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 #19
Source Project: java-technology-stack   Author: codeEngraver   File: ServletAnnotationControllerHandlerMethodTests.java    License: MIT License 5 votes vote down vote up
@InitBinder
public 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 #20
Source Project: java-technology-stack   Author: codeEngraver   File: ServletAnnotationControllerHandlerMethodTests.java    License: MIT License 5 votes vote down vote up
@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 #21
Source Project: java-technology-stack   Author: codeEngraver   File: ServletAnnotationControllerHandlerMethodTests.java    License: MIT License 5 votes vote down vote up
@Override
public void initBinder(WebDataBinder binder) {
	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 #22
Source Project: java-technology-stack   Author: codeEngraver   File: ServletAnnotationControllerHandlerMethodTests.java    License: MIT License 5 votes vote down vote up
@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 #23
Source Project: java-technology-stack   Author: codeEngraver   File: BindTagTests.java    License: MIT License 5 votes vote down vote up
@Test
public void transformTagWithHtmlEscape() throws JspException {
	// first set up the PageContext and the bean
	PageContext pc = createPageContext();
	TestBean tb = new TestBean();
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
	CustomDateEditor l = new CustomDateEditor(df, true);
	binder.registerCustomEditor(Date.class, l);
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult());

	// try another time, this time using Strings
	BindTag bind = new BindTag();
	bind.setPageContext(pc);
	bind.setPath("tb.name");
	bind.doStartTag();

	TransformTag transform = new TransformTag();
	transform.setPageContext(pc);
	transform.setValue("na<me");
	transform.setParent(bind);
	transform.setVar("theString");
	transform.setHtmlEscape(true);
	transform.doStartTag();

	assertNotNull(pc.getAttribute("theString"));
	assertEquals("na&lt;me", pc.getAttribute("theString"));
}
 
Example #24
Source Project: java-technology-stack   Author: codeEngraver   File: ConcurrentBeanFactoryTests.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	Assume.group(TestGroup.PERFORMANCE);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(CONTEXT);
	factory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Date.class, new CustomDateEditor((DateFormat) DATE_FORMAT.clone(), false));
		}
	});
	this.factory = factory;
}
 
Example #25
Source Project: bbs   Author: diyhi   File: MyBindingInitializer.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public void initBinder(WebDataBinder binder) {  
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
      dateFormat.setLenient(false);   
      binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
   //   binder.registerCustomEditor(String.class, new StringTrimmerEditor(false)); 

  }
 
Example #26
Source Project: SA47   Author: suriarasai   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 #27
Source Project: Roothub   Author: miansen   File: UserAdminController.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 局部日期转换,将 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 #28
Source Project: Roothub   Author: miansen   File: ConvertDateConfig.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
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 #29
Source Project: xmanager   Author: freezek   File: BaseController.java    License: Apache License 2.0 5 votes vote down vote up
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
    /**
     * 自动转换日期类型的字段格式
     */
    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    /**
     * 防止XSS攻击
     */
    binder.registerCustomEditor(String.class, new StringEscapeEditor());
}
 
Example #30
Source Project: xmanager   Author: freezek   File: BaseController.java    License: Apache License 2.0 5 votes vote down vote up
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
    /**
     * 自动转换日期类型的字段格式
     */
    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    /**
     * 防止XSS攻击
     */
    binder.registerCustomEditor(String.class, new StringEscapeEditor());
}