@Override protected void renderDefaultContent(TagWriter tagWriter) throws JspException { tagWriter.startTag(getElement()); writeDefaultAttributes(tagWriter); String delimiter = ObjectUtils.getDisplayString(evaluate("delimiter", getDelimiter())); String[] errorMessages = getBindStatus().getErrorMessages(); for (int i = 0; i < errorMessages.length; i++) { String errorMessage = errorMessages[i]; if (i > 0) { tagWriter.appendValue(delimiter); } tagWriter.appendValue(getDisplayString(errorMessage)); } tagWriter.endTag(); }
@Test public void withoutErrorsInstance() throws Exception { int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertEquals(0, output.length()); }
/** * Get the value for the HTML '{@code id}' attribute. * <p>Appends '{@code .errors}' to the value returned by {@link #getPropertyPath()} * or to the model attribute name if the {@code <form:errors/>} tag's * '{@code path}' attribute has been omitted. * @return the value for the HTML '{@code id}' attribute * @see #getPropertyPath() */ @Override protected String autogenerateId() throws JspException { String path = getPropertyPath(); if ("".equals(path) || "*".equals(path)) { path = (String) this.pageContext.getAttribute( FormTag.MODEL_ATTRIBUTE_VARIABLE_NAME, PageContext.REQUEST_SCOPE); } return StringUtils.deleteAny(path, "[]") + ".errors"; }
@Override @SuppressWarnings("serial") protected void onSetUp() { this.tag = new ErrorsTag() { @Override protected TagWriter createTagWriter() { return new TagWriter(getWriter()); } }; this.tag.setPath("name"); this.tag.setParent(getFormTag()); this.tag.setPageContext(getPageContext()); }
pageContext.getOut().print( "</div>" ); ErrorsTag errorsTag = new ErrorsTag(); errorsTag.setParent( this ); errorsTag.setPageContext( pageContext ); errorsTag.setPath( path ); errorsTag.doStartTag(); JspWriter out = pageContext.pushBody(); out.print( "<span class=\"zsa-validationerror-flag\"></span>" ); errorsTag.setBodyContent( ( BodyContent )out ); errorsTag.doInitBody(); errorsTag.doAfterBody(); errorsTag.doEndTag(); out = pageContext.popBody(); errorsTag.release(); return EVAL_PAGE;
ErrorsTag errorsTag = new ErrorsTag(); errorsTag.setPageContext( metawidgetTag.getPageContext() ); errorsTag.setCssStyle( mErrorStyle ); errorsTag.setCssClass( mErrorStyleClass ); errorsTag.setPath( path ); errorsTag.doStartTag(); errorsTag.doEndTag();
@Test public void specificPathMatchesSpecificFieldOnly() throws Exception { this.tag.setPath("name"); Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); errors.reject("some.code", "object error"); errors.rejectValue("name", "some.code", "field error"); exposeBindingResult(errors); this.tag.doStartTag(); assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); this.tag.doEndTag(); String output = getOutput(); assertTrue(output.contains("id=\"name.errors\"")); assertFalse(output.contains("object error")); assertTrue(output.contains("field error")); }
@Test public void asBodyTag() throws Exception { Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); errors.rejectValue("name", "some.code", "Default Message"); errors.rejectValue("name", "too.short", "Too Short"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); String bodyContent = "Foo"; this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter())); this.tag.doEndTag(); this.tag.doFinally(); assertEquals(bodyContent, getOutput()); assertNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); }
@Test public void withExplicitNonWhitespaceBodyContent() throws Exception { String mockContent = "This is some explicit body content"; this.tag.setBodyContent(new MockBodyContent(mockContent, getWriter())); // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default Message"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); assertEquals(mockContent, getOutput()); }
@Override protected void renderDefaultContent(TagWriter tagWriter) throws JspException { tagWriter.startTag(getElement()); writeDefaultAttributes(tagWriter); String delimiter = ObjectUtils.getDisplayString(evaluate("delimiter", getDelimiter())); String[] errorMessages = getBindStatus().getErrorMessages(); for (int i = 0; i < errorMessages.length; i++) { String errorMessage = errorMessages[i]; if (i > 0) { tagWriter.appendValue(delimiter); } tagWriter.appendValue(getDisplayString(errorMessage)); } tagWriter.endTag(); }
@Test public void withErrorsAndDynamicAttributes() throws Exception { String dynamicAttribute1 = "attr1"; String dynamicAttribute2 = "attr2"; this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1); this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2); // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default Message"); errors.rejectValue("name", "too.short", "Too Short"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertElementTagOpened(output); assertElementTagClosed(output); assertContainsAttribute(output, "id", "name.errors"); assertContainsAttribute(output, dynamicAttribute1, dynamicAttribute1); assertContainsAttribute(output, dynamicAttribute2, dynamicAttribute2); assertBlockTagContains(output, "<br/>"); assertBlockTagContains(output, "Default Message"); assertBlockTagContains(output, "Too Short"); }
@Test public void withNonEscapedErrors() throws Exception { this.tag.setHtmlEscape(false); // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default <> Message"); errors.rejectValue("name", "too.short", "Too & Short"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertElementTagOpened(output); assertElementTagClosed(output); assertContainsAttribute(output, "id", "name.errors"); assertBlockTagContains(output, "<br/>"); assertBlockTagContains(output, "Default <> Message"); assertBlockTagContains(output, "Too & Short"); }
@Test public void withErrorsAndCustomElement() throws Exception { // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default Message"); errors.rejectValue("name", "too.short", "Too Short"); exposeBindingResult(errors); this.tag.setElement("div"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertElementTagOpened(output); assertElementTagClosed(output); assertContainsAttribute(output, "id", "name.errors"); assertBlockTagContains(output, "<br/>"); assertBlockTagContains(output, "Default Message"); assertBlockTagContains(output, "Too Short"); }
private void assertElementTagOpened(String output) { assertTrue(output.startsWith("<" + this.tag.getElement() + " ")); }
@Test public void starMatchesAllErrors() throws Exception { this.tag.setPath("*"); Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); errors.reject("some.code", "object error"); errors.rejectValue("name", "some.code", "field error"); exposeBindingResult(errors); this.tag.doStartTag(); assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); this.tag.doEndTag(); String output = getOutput(); assertTrue(output.contains("id=\"testBean.errors\"")); assertTrue(output.contains("object error")); assertTrue(output.contains("field error")); }
@Test public void asBodyTagWithExistingMessagesAttribute() throws Exception { String existingAttribute = "something"; getPageContext().setAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, existingAttribute); Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); errors.rejectValue("name", "some.code", "Default Message"); errors.rejectValue("name", "too.short", "Too Short"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); assertTrue(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE) instanceof List); String bodyContent = "Foo"; this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter())); this.tag.doEndTag(); this.tag.doFinally(); assertEquals(bodyContent, getOutput()); assertEquals(existingAttribute, getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); }
@Test public void withExplicitEmptyWhitespaceBodyContent() throws Exception { this.tag.setBodyContent(new MockBodyContent("", getWriter())); // construct an errors instance of the tag TestBean target = new TestBean(); target.setName("Rob Harrop"); Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME); errors.rejectValue("name", "some.code", "Default Message"); exposeBindingResult(errors); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertElementTagOpened(output); assertElementTagClosed(output); assertContainsAttribute(output, "id", "name.errors"); assertBlockTagContains(output, "Default Message"); }
private void assertElementTagClosed(String output) { assertTrue(output.endsWith("</" + this.tag.getElement() + ">")); }
@Override protected void renderDefaultContent(TagWriter tagWriter) throws JspException { tagWriter.startTag(getElement()); writeDefaultAttributes(tagWriter); String delimiter = ObjectUtils.getDisplayString(evaluate("delimiter", getDelimiter())); String[] errorMessages = getBindStatus().getErrorMessages(); for (int i = 0; i < errorMessages.length; i++) { String errorMessage = errorMessages[i]; if (i > 0) { tagWriter.appendValue(delimiter); } tagWriter.appendValue(getDisplayString(errorMessage)); } tagWriter.endTag(); }
/** * https://jira.spring.io/browse/SPR-4005 */ @Test public void omittedPathMatchesObjectErrorsOnly() throws Exception { this.tag.setPath(null); Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME"); errors.reject("some.code", "object error"); errors.rejectValue("name", "some.code", "field error"); exposeBindingResult(errors); this.tag.doStartTag(); assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE)); this.tag.doEndTag(); String output = getOutput(); assertTrue(output.contains("id=\"testBean.errors\"")); assertTrue(output.contains("object error")); assertFalse(output.contains("field error")); }