Search in sources :

Example 21 with ServletRequestDataBinder

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

the class BindTagTests method bindStatusGetErrorMessagesAsString.

@Test
public void bindStatusGetErrorMessagesAsString() throws JspException {
    // one error (should not include delimiter)
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    errors.reject("code1", null, "message1");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    tag.doStartTag();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertEquals("Error messages String should be 'message1'", status.getErrorMessagesAsString(","), "message1");
    // two errors
    pc = createPageContext();
    errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    errors.reject("code1", null, "message1");
    errors.reject("code1", null, "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    tag.doStartTag();
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertEquals("Error messages String should be 'message1,message2'", status.getErrorMessagesAsString(","), "message1,message2");
    // no errors
    pc = createPageContext();
    errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    tag.doStartTag();
    status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertEquals("Error messages String should be ''", 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 22 with ServletRequestDataBinder

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

the class BindTagTests method bindTagWithIndexedPropertiesAndCustomEditor.

@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
    PageContext pc = createPageContext();
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new ServletRequestDataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {

        @Override
        public String getAsText() {
            return "something";
        }
    });
    Errors errors = binder.getBindingResult();
    errors.rejectValue("array[0]", "code1", "message1");
    errors.rejectValue("array[0]", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.array[0]");
    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", "array[0]".equals(status.getExpression()));
    // because of the custom editor getValue() should return a String
    assertTrue("Value is TestBean", status.getValue() instanceof String);
    assertTrue("Correct value", "something".equals(status.getValue()));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) Errors(org.springframework.validation.Errors) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) DataBinder(org.springframework.validation.DataBinder) BindStatus(org.springframework.web.servlet.support.BindStatus) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 23 with ServletRequestDataBinder

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

the class BindTagTests method bindTagWithFieldErrorsAndNoDefaultMessage.

@Test
public void bindTagWithFieldErrorsAndNoDefaultMessage() throws JspException {
    PageContext pc = createPageContext();
    TestBean tb = new TestBean();
    tb.setName("name1");
    Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
    errors.rejectValue("name", "code1");
    errors.rejectValue("name", "code2");
    errors.rejectValue("age", "code2");
    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 errorCode", "code1".equals(status.getErrorCode()));
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
    assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
    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 errorCode", "code2".equals(status.getErrorCode()));
    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 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]));
}
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 24 with ServletRequestDataBinder

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

the class BindTagTests method bindTagWithGlobalErrorsAndNoDefaultMessage.

@Test
public void bindTagWithGlobalErrorsAndNoDefaultMessage() throws JspException {
    PageContext pc = createPageContext();
    Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
    errors.reject("code1");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb");
    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", status.getExpression() == null);
    assertTrue("Correct value", status.getValue() == null);
    assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
    assertTrue("Correct isError", status.isError());
    assertTrue("Correct errorCodes", status.getErrorCodes().length == 1);
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
    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 == 1);
    assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
}
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 25 with ServletRequestDataBinder

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

the class BindTagTests method nestingInFormTag.

/**
	 * SPR-4022
	 */
@SuppressWarnings("serial")
@Test
public void nestingInFormTag() throws JspException {
    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());
    FormTag formTag = new FormTag() {

        @Override
        protected TagWriter createTagWriter() {
            return new TagWriter(new StringWriter());
        }
    };
    String action = "/form.html";
    String commandName = "tb";
    String name = "formName";
    String enctype = "my/enctype";
    String method = "POST";
    String onsubmit = "onsubmit";
    String onreset = "onreset";
    String cssClass = "myClass";
    String cssStyle = "myStyle";
    String acceptCharset = "iso-8859-1";
    formTag.setName(name);
    formTag.setCssClass(cssClass);
    formTag.setCssStyle(cssStyle);
    formTag.setAction(action);
    formTag.setModelAttribute(commandName);
    formTag.setEnctype(enctype);
    formTag.setMethod(method);
    formTag.setOnsubmit(onsubmit);
    formTag.setOnreset(onreset);
    formTag.setAcceptCharset(acceptCharset);
    formTag.setPageContext(pc);
    formTag.doStartTag();
    BindTag bindTag1 = new BindTag();
    bindTag1.setPageContext(pc);
    bindTag1.setPath("date");
    bindTag1.doStartTag();
    bindTag1.doEndTag();
    BindTag bindTag2 = new BindTag();
    bindTag2.setPageContext(pc);
    bindTag2.setPath("tb.date");
    bindTag2.doStartTag();
    bindTag2.doEndTag();
    BindTag bindTag3 = new BindTag();
    bindTag3.setPageContext(pc);
    bindTag3.setPath("tb");
    bindTag3.doStartTag();
    bindTag3.doEndTag();
    formTag.doEndTag();
}
Also used : FormTag(org.springframework.web.servlet.tags.form.FormTag) StringWriter(java.io.StringWriter) 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) TagWriter(org.springframework.web.servlet.tags.form.TagWriter) PageContext(javax.servlet.jsp.PageContext) SimpleDateFormat(java.text.SimpleDateFormat) 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