/** * Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting. * @param path the bean and property path for which values and errors will be resolved (e.g. "person.age") * @param htmlEscape create a BindStatus with automatic HTML escaping? * @return the new BindStatus instance * @throws IllegalStateException if no corresponding Errors object found */ public BindStatus getBindStatus(String path, boolean htmlEscape) throws IllegalStateException { return new BindStatus(this, path, htmlEscape); }
/** * Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting. * @param path the bean and property path for which values and errors will be resolved (e.g. "person.age") * @return the new BindStatus instance * @throws IllegalStateException if no corresponding Errors object found */ public BindStatus getBindStatus(String path) throws IllegalStateException { return new BindStatus(this, path, isDefaultHtmlEscape()); }
/** * Get the {@link BindStatus} for this tag. */ protected BindStatus getBindStatus() throws JspException { if (this.bindStatus == null) { // HTML escaping in tags is performed by the ValueFormatter class. String nestedPath = getNestedPath(); String pathToUse = (nestedPath != null ? nestedPath + getPath() : getPath()); if (pathToUse.endsWith(PropertyAccessor.NESTED_PROPERTY_SEPARATOR)) { pathToUse = pathToUse.substring(0, pathToUse.length() - 1); } this.bindStatus = new BindStatus(getRequestContext(), pathToUse, false); } return this.bindStatus; }
@Override protected final int doStartTagInternal() throws Exception { String resolvedPath = getPath(); if (!isIgnoreNestedPath()) { String nestedPath = (String) this.pageContext.getAttribute( NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE); // only prepend if not already an absolute path if (nestedPath != null && !resolvedPath.startsWith(nestedPath) && !resolvedPath.equals(nestedPath.substring(0, nestedPath.length() - 1))) { resolvedPath = nestedPath + resolvedPath; } } try { this.status = new BindStatus(getRequestContext(), resolvedPath, isHtmlEscape()); } catch (IllegalStateException ex) { throw new JspTagException(ex.getMessage()); } // Save previous status values, for re-exposure at the end of this tag. this.previousPageStatus = this.pageContext.getAttribute(STATUS_VARIABLE_NAME, PageContext.PAGE_SCOPE); this.previousRequestStatus = this.pageContext.getAttribute(STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); // Expose this tag's status object as PageContext attribute, // making it available for JSP EL. this.pageContext.removeAttribute(STATUS_VARIABLE_NAME, PageContext.PAGE_SCOPE); this.pageContext.setAttribute(STATUS_VARIABLE_NAME, this.status, PageContext.REQUEST_SCOPE); return EVAL_BODY_INCLUDE; }
/** * @see org.springframework.web.servlet.support.RequestContext#getBindStatus(String, boolean) */ public BindStatus getBindStatus(String path, boolean htmlEscape) throws IllegalStateException { return new BindStatus(new RequestContext(this.request), path, true); }
/** * @see org.springframework.web.servlet.support.RequestContext#getBindStatus(String) */ public BindStatus getBindStatus(String path) throws IllegalStateException { return new BindStatus(new RequestContext(this.request), path, false); }
@Test public void withItemsNullReference() throws Exception { getPageContext().setAttribute( SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.country", false)); this.tag.setItems(Collections.emptyList()); this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); List children = rootElement.elements(); assertEquals("Incorrect number of children", 0, children.size()); }
@Test public void withCollection() throws Exception { getPageContext().setAttribute( SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.country", false)); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); this.tag.setId("myOption"); this.tag.setCssClass("myClass"); this.tag.setOnclick("CLICK"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); List children = rootElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element element = (Element) rootElement.selectSingleNode("option[@value = 'UK']"); assertEquals("UK node not selected", "selected", element.attribute("selected").getValue()); assertEquals("myOption3", element.attribute("id").getValue()); assertEquals("myClass", element.attribute("class").getValue()); assertEquals("CLICK", element.attribute("onclick").getValue()); }
@Test @SuppressWarnings("rawtypes") public void withJavaEnum() throws Exception { GenericBean testBean = new GenericBean(); testBean.setCustomEnum(CustomEnum.VALUE_1); getPageContext().getRequest().setAttribute("testBean", testBean); String selectName = "testBean.customEnum"; getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false)); this.tag.setValue("VALUE_1"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getWriter().toString(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "VALUE_1"); assertContainsAttribute(output, "selected", "selected"); }
@Test public void withCustomObjectSelected() throws Exception { String selectName = "testBean.someNumber"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue(new Float(12.34)); this.tag.setLabel("GBP 12.34"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "12.34"); assertContainsAttribute(output, "selected", "selected"); assertBlockTagContains(output, "GBP 12.34"); }
@Test public void withCustomObjectNotSelected() throws Exception { String selectName = "testBean.someNumber"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue(new Float(12.35)); this.tag.setLabel("GBP 12.35"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "12.35"); assertAttributeNotPresent(output, "selected"); assertBlockTagContains(output, "GBP 12.35"); }
@Test public void multiBind() throws Exception { BeanPropertyBindingResult result = new BeanPropertyBindingResult(new TestBean(), "testBean"); result.getPropertyAccessor().registerCustomEditor(TestBean.class, "friends", new FriendEditor()); exposeBindingResult(result); BindStatus bindStatus = new BindStatus(getRequestContext(), "testBean.friends", false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue(new TestBean("foo")); this.tag.doStartTag(); this.tag.doEndTag(); assertEquals("<option value=\"foo\">foo</option>", getOutput()); }
@Test public void asBodyTagSelected() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); String bodyContent = "some content"; this.tag.setValue("Rob Harrop"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter())); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertBlockTagContains(output, bodyContent); }
@Test public void renderNotSelected() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue("bar"); this.tag.setLabel("Bar"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "bar"); assertBlockTagContains(output, "Bar"); }
@Test public void asBodyTagCollapsed() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); String bodyContent = "some content"; this.tag.setValue(bodyContent); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter())); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", bodyContent); assertBlockTagContains(output, bodyContent); }
@Test public void asBodyTag() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); String bodyContent = "some content"; this.tag.setValue("foo"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter())); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "selected", "selected"); assertBlockTagContains(output, bodyContent); }
@Test public void canBeDisabledEvenWhenSelected() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue("bar"); this.tag.setLabel("Bar"); this.tag.setDisabled(true); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "bar"); assertContainsAttribute(output, "disabled", "disabled"); assertBlockTagContains(output, "Bar"); }
@Test public void renderSelected() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setId("myOption"); this.tag.setValue("foo"); this.tag.setLabel("Foo"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "id", "myOption"); assertContainsAttribute(output, "value", "foo"); assertContainsAttribute(output, "selected", "selected"); assertBlockTagContains(output, "Foo"); }
@Test public void withNoLabel() throws Exception { String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue("bar"); this.tag.setCssClass("myClass"); this.tag.setOnclick("CLICK"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "bar"); assertContainsAttribute(output, "class", "myClass"); assertContainsAttribute(output, "onclick", "CLICK"); assertBlockTagContains(output, "bar"); }
@Test public void renderWithDynamicAttributes() throws Exception { String dynamicAttribute1 = "attr1"; String dynamicAttribute2 = "attr2"; String selectName = "testBean.name"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue("bar"); this.tag.setLabel("Bar"); this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1); this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "value", "bar"); assertContainsAttribute(output, dynamicAttribute1, dynamicAttribute1); assertContainsAttribute(output, dynamicAttribute2, dynamicAttribute2); assertBlockTagContains(output, "Bar"); }