下面列出了java.beans.PropertyEditorSupport#org.springframework.beans.propertyeditors.CustomDateEditor 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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"));
}
@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"));
}
@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
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));
}
@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));
}
@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));
}
@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));
}
@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));
}
@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<me", pc.getAttribute("theString"));
}
@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;
}
@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));
}
@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));
}
@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));
}
@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));
}
@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));
}
@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));
}
@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
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));
}
@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));
}
@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));
}
@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));
}
@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));
}
@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<me", pc.getAttribute("theString"));
}
@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;
}
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));
}
@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));
}
@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));
}
@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());
}
@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());
}