Search in sources :

Example 6 with CustomDateEditor

use of org.springframework.beans.propertyeditors.CustomDateEditor in project spring-framework by spring-projects.

the class BindTagTests method transformTagWithSettingOfScope.

@Test
public void transformTagWithSettingOfScope() 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());
    // execute the bind tag using the date property
    BindTag bind = new BindTag();
    bind.setPageContext(pc);
    bind.setPath("tb.date");
    bind.doStartTag();
    // transform stuff
    TransformTag transform = new TransformTag();
    transform.setPageContext(pc);
    transform.setParent(bind);
    transform.setValue(tb.getDate());
    transform.setVar("theDate");
    transform.setScope("page");
    transform.doStartTag();
    transform.release();
    assertNotNull(pc.getAttribute("theDate"));
    assertEquals(df.format(tb.getDate()), pc.getAttribute("theDate"));
    // try another time, this time using Strings
    bind = new BindTag();
    bind.setPageContext(pc);
    bind.setPath("tb.name");
    bind.doStartTag();
    transform = new TransformTag();
    transform.setPageContext(pc);
    transform.setValue("name");
    transform.setParent(bind);
    transform.setVar("theString");
    transform.setScope("page");
    transform.doStartTag();
    transform.release();
    assertNotNull(pc.getAttribute("theString"));
    assertEquals("name", pc.getAttribute("theString"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) CustomDateEditor(org.springframework.beans.propertyeditors.CustomDateEditor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 7 with CustomDateEditor

use of org.springframework.beans.propertyeditors.CustomDateEditor in project spring-framework by spring-projects.

the class BindTagTests method transformTagCorrectBehavior.

@Test
public void transformTagCorrectBehavior() 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());
    // execute the bind tag using the date property
    BindTag bind = new BindTag();
    bind.setPageContext(pc);
    bind.setPath("tb.date");
    bind.doStartTag();
    // transform stuff
    TransformTag transform = new TransformTag();
    transform.setPageContext(pc);
    transform.setParent(bind);
    transform.setValue(tb.getDate());
    transform.setVar("theDate");
    transform.doStartTag();
    assertNotNull(pc.getAttribute("theDate"));
    assertEquals(pc.getAttribute("theDate"), df.format(tb.getDate()));
    // try another time, this time using Strings
    bind = new BindTag();
    bind.setPageContext(pc);
    bind.setPath("tb.name");
    bind.doStartTag();
    transform = new TransformTag();
    transform.setPageContext(pc);
    transform.setValue("name");
    transform.setParent(bind);
    transform.setVar("theString");
    transform.doStartTag();
    assertNotNull(pc.getAttribute("theString"));
    assertEquals("name", pc.getAttribute("theString"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) CustomDateEditor(org.springframework.beans.propertyeditors.CustomDateEditor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 8 with CustomDateEditor

use of org.springframework.beans.propertyeditors.CustomDateEditor in project spring-framework by spring-projects.

the class BindTagTests method transformTagNonExistingValue.

@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"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) CustomDateEditor(org.springframework.beans.propertyeditors.CustomDateEditor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 9 with CustomDateEditor

use of org.springframework.beans.propertyeditors.CustomDateEditor in project spring-framework by spring-projects.

the class BindTagTests method transformTagWithHtmlEscape.

@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"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) CustomDateEditor(org.springframework.beans.propertyeditors.CustomDateEditor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 10 with CustomDateEditor

use of org.springframework.beans.propertyeditors.CustomDateEditor in project spring-framework by spring-projects.

the class CustomEditorConfigurerTests method testCustomEditorConfigurerWithPropertyEditorRegistrar.

@Test
public void testCustomEditorConfigurerWithPropertyEditorRegistrar() throws ParseException {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    CustomEditorConfigurer cec = new CustomEditorConfigurer();
    final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
    cec.setPropertyEditorRegistrars(new PropertyEditorRegistrar[] { new PropertyEditorRegistrar() {

        @Override
        public void registerCustomEditors(PropertyEditorRegistry registry) {
            registry.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
        }
    } });
    cec.postProcessBeanFactory(bf);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("date", "2.12.1975");
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setPropertyValues(pvs);
    bf.registerBeanDefinition("tb1", bd1);
    pvs = new MutablePropertyValues();
    pvs.add("someMap[myKey]", new TypedStringValue("2.12.1975", Date.class));
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setPropertyValues(pvs);
    bf.registerBeanDefinition("tb2", bd2);
    TestBean tb1 = (TestBean) bf.getBean("tb1");
    assertEquals(df.parse("2.12.1975"), tb1.getDate());
    TestBean tb2 = (TestBean) bf.getBean("tb2");
    assertEquals(df.parse("2.12.1975"), tb2.getSomeMap().get("myKey"));
}
Also used : PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) TestBean(org.springframework.tests.sample.beans.TestBean) CustomDateEditor(org.springframework.beans.propertyeditors.CustomDateEditor) DateFormat(java.text.DateFormat) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Date(java.util.Date) Test(org.junit.Test)

Aggregations

CustomDateEditor (org.springframework.beans.propertyeditors.CustomDateEditor)12 SimpleDateFormat (java.text.SimpleDateFormat)10 DateFormat (java.text.DateFormat)7 Test (org.junit.Test)7 TestBean (org.springframework.tests.sample.beans.TestBean)7 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)7 PageContext (javax.servlet.jsp.PageContext)6 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)6 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)6 InitBinder (org.springframework.web.bind.annotation.InitBinder)3 Date (java.util.Date)2 PropertyEditorRegistrar (org.springframework.beans.PropertyEditorRegistrar)2 PropertyEditorRegistry (org.springframework.beans.PropertyEditorRegistry)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 TodoPriorityPropertyEditor (com.in28minutes.springmvc.web.util.TodoPriorityPropertyEditor)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 JspException (javax.servlet.jsp.JspException)1 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)1 SdvFilterDataBean (org.akaza.openclinica.controller.helper.SdvFilterDataBean)1