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