@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()); }
@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 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")); }
/** * 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")); }
errorsTag.setPath( path );
errorsTag.setParent( this ); errorsTag.setPageContext( pageContext ); errorsTag.setPath( path ); errorsTag.doStartTag(); JspWriter out = pageContext.pushBody();