Search in sources :

Example 16 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithFieldErrorsAndDefaultMessageOnly.

@Test
public void bindTagWithFieldErrorsAndDefaultMessageOnly() throws JspException {
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    tb.setName("name1");
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("name", null, "message & 1");
    errors.rejectValue("name", null, "message2");
    errors.rejectValue("age", null, "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.name");
    tag.setHtmlEscape(true);
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "name".equals(status.getExpression()));
    assertTrue("Correct value", "name1".equals(status.getValue()));
    assertTrue("Correct displayValue", "name1".equals(status.getDisplayValue()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 2);
    assertTrue("Correct errorMessage", "message & 1".equals(status.getErrorMessage()));
    assertTrue("Correct errorMessage", "message & 1".equals(status.getErrorMessages()[0]));
    assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
    assertTrue("Correct errorMessagesAsString", "message & 1 - message2".equals(status.getErrorMessagesAsString(" - ")));
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.age");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "age".equals(status.getExpression()));
    assertTrue("Correct value", new Integer(0).equals(status.getValue()));
    assertTrue("Correct displayValue", "0".equals(status.getDisplayValue()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
    assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessage()));
    assertTrue("Correct errorMessagesAsString", "message2".equals(status.getErrorMessagesAsString(" - ")));
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.*");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "*".equals(status.getExpression()));
    assertTrue("Correct value", status.getValue() == null);
    assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 3);
    assertTrue("Correct errorMessage", "message & 1".equals(status.getErrorMessage()));
    assertTrue("Correct errorMessage", "message & 1".equals(status.getErrorMessages()[0]));
    assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
    assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[2]));
}
Also used : Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 17 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project spring-framework by spring-projects.

the class BindTagTests method bindErrorsTagWithoutErrors.

@Test
public void bindErrorsTagWithoutErrors() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindErrorsTag tag = new BindErrorsTag();
    tag.setPageContext(pc);
    tag.setName("tb");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.SKIP_BODY);
    assertTrue("Doesn't have errors variable", pc.getAttribute(BindErrorsTag.ERRORS_VARIABLE_NAME) == null);
}
Also used : Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) Test(org.junit.Test)

Example 18 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project spring-framework by spring-projects.

the class BindTagTests method transformTagOutsideBindTag.

@Test
public void transformTagOutsideBindTag() 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());
    // now try to execute the tag outside a bindtag
    TransformTag transform = new TransformTag();
    transform.setPageContext(pc);
    transform.setVar("var");
    transform.setValue("bla");
    try {
        transform.doStartTag();
        fail("Tag can be executed outside BindTag");
    } catch (JspException e) {
    // this is ok!
    }
    // now try to execute the tag outside a bindtag, but inside a messageTag
    MessageTag message = new MessageTag();
    message.setPageContext(pc);
    transform = new TransformTag();
    transform.setPageContext(pc);
    transform.setVar("var");
    transform.setValue("bla");
    transform.setParent(message);
    try {
        transform.doStartTag();
        fail("Tag can be executed outside BindTag and inside messagtag");
    } catch (JspException e) {
    // this is ok!
    }
}
Also used : JspException(javax.servlet.jsp.JspException) 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 19 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithNestedFieldErrors.

@Test
public void bindTagWithNestedFieldErrors() throws JspException {
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    tb.setName("name1");
    TestBean spouse = new TestBean();
    spouse.setName("name2");
    tb.setSpouse(spouse);
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("spouse.name", "code1", "message1");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.spouse.name");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "spouse.name".equals(status.getExpression()));
    assertTrue("Correct value", "name2".equals(status.getValue()));
    assertTrue("Correct displayValue", "name2".equals(status.getDisplayValue()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorCodes", status.getErrorCodes().length == 1);
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
    assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage()));
    assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(" - ")));
}
Also used : Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 20 with ServletRequestDataBinder

use of org.springframework.web.bind.ServletRequestDataBinder in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithFieldErrors.

@Test
public void bindTagWithFieldErrors() throws JspException {
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    tb.setName("name1");
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("name", "code1", "message & 1");
    errors.rejectValue("name", "code2", "message2");
    errors.rejectValue("age", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.name");
    tag.setHtmlEscape(true);
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "name".equals(status.getExpression()));
    assertTrue("Correct value", "name1".equals(status.getValue()));
    assertTrue("Correct displayValue", "name1".equals(status.getDisplayValue()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorCodes", status.getErrorCodes().length == 2);
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 2);
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
    assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
    assertTrue("Correct errorMessage", "message & 1".equals(status.getErrorMessage()));
    assertTrue("Correct errorMessage", "message & 1".equals(status.getErrorMessages()[0]));
    assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
    assertTrue("Correct errorMessagesAsString", "message & 1 - message2".equals(status.getErrorMessagesAsString(" - ")));
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.age");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "age".equals(status.getExpression()));
    assertTrue("Correct value", new Integer(0).equals(status.getValue()));
    assertTrue("Correct displayValue", "0".equals(status.getDisplayValue()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorCodes", status.getErrorCodes().length == 1);
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
    assertTrue("Correct errorCode", "code2".equals(status.getErrorCode()));
    assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessage()));
    assertTrue("Correct errorMessagesAsString", "message2".equals(status.getErrorMessagesAsString(" - ")));
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.*");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "*".equals(status.getExpression()));
    assertTrue("Correct value", status.getValue() == null);
    assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorCodes", status.getErrorCodes().length == 3);
    assertTrue("Correct errorMessages", status.getErrorMessages().length == 3);
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
    assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
    assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[2]));
    assertTrue("Correct errorMessage", "message & 1".equals(status.getErrorMessage()));
    assertTrue("Correct errorMessage", "message & 1".equals(status.getErrorMessages()[0]));
    assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
    assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[2]));
}
Also used : Errors(org.springframework.validation.Errors) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Aggregations

ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)29 Test (org.junit.Test)25 TestBean (org.springframework.tests.sample.beans.TestBean)24 PageContext (javax.servlet.jsp.PageContext)22 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)22 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)21 Errors (org.springframework.validation.Errors)16 BindStatus (org.springframework.web.servlet.support.BindStatus)14 SimpleDateFormat (java.text.SimpleDateFormat)7 CustomDateEditor (org.springframework.beans.propertyeditors.CustomDateEditor)7 DateFormat (java.text.DateFormat)6 WebDataBinder (org.springframework.web.bind.WebDataBinder)3 HashMap (java.util.HashMap)2 PropertyEditorSupport (java.beans.PropertyEditorSupport)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 ServletRequest (javax.servlet.ServletRequest)1 JspException (javax.servlet.jsp.JspException)1 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)1 SdvFilterDataBean (org.akaza.openclinica.controller.helper.SdvFilterDataBean)1