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]));
}
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);
}
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!
}
}
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(" - ")));
}
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]));
}
Aggregations