@Override protected BindStatus getBindStatus() { return (BindStatus) this.pageContext.getAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE); }
@Override protected BindStatus getBindStatus() { return (BindStatus) this.pageContext.getAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE); }
/** * Get the next unique ID (within the given {@link PageContext}) for the supplied name. */ public static String nextId(String name, PageContext pageContext) { String attributeName = PAGE_CONTEXT_ATTRIBUTE_PREFIX + name; Integer currentCount = (Integer) pageContext.getAttribute(attributeName); currentCount = (currentCount != null ? currentCount + 1 : 1); pageContext.setAttribute(attributeName, currentCount); return (name + currentCount); }
/** * Get the value of the nested path that may have been exposed by the * {@link NestedPathTag}. */ @Nullable protected String getNestedPath() { return (String) this.pageContext.getAttribute(NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE); }
/** * Exposes any bind status error messages under {@link #MESSAGES_ATTRIBUTE this key} * in the {@link PageContext#PAGE_SCOPE}. * <p>Only called if {@link #shouldRender()} returns {@code true}. * @see #removeAttributes() */ @Override protected void exposeAttributes() throws JspException { List<String> errorMessages = new ArrayList<>(); errorMessages.addAll(Arrays.asList(getBindStatus().getErrorMessages())); this.oldMessages = this.pageContext.getAttribute(MESSAGES_ATTRIBUTE, PageContext.PAGE_SCOPE); this.pageContext.setAttribute(MESSAGES_ATTRIBUTE, errorMessages, PageContext.PAGE_SCOPE); this.errorMessagesWereExposed = true; }
@Override public int doStartTag() throws JspException { // Save previous nestedPath value, build and expose current nestedPath value. // Use request scope to expose nestedPath to included pages too. this.previousNestedPath = (String) this.pageContext.getAttribute(NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE); String nestedPath = (this.previousNestedPath != null ? this.previousNestedPath + getPath() : getPath()); this.pageContext.setAttribute(NESTED_PATH_VARIABLE_NAME, nestedPath, PageContext.REQUEST_SCOPE); return EVAL_BODY_INCLUDE; }
@Override protected void exposeAttributes() throws JspException { Object value = resolveValue(); this.oldValue = this.pageContext.getAttribute(VALUE_VARIABLE_NAME); this.pageContext.setAttribute(VALUE_VARIABLE_NAME, value); this.oldDisplayValue = this.pageContext.getAttribute(DISPLAY_VALUE_VARIABLE_NAME); this.pageContext.setAttribute(DISPLAY_VALUE_VARIABLE_NAME, getDisplayString(value, getBindStatus().getEditor())); }
@Override protected void renderFromBodyContent(BodyContent bodyContent, TagWriter tagWriter) throws JspException { Object value = this.pageContext.getAttribute(VALUE_VARIABLE_NAME); String label = bodyContent.getString(); renderOption(value, label, tagWriter); }
@Override protected void renderDefaultContent(TagWriter tagWriter) throws JspException { Object value = this.pageContext.getAttribute(VALUE_VARIABLE_NAME); String label = getLabelValue(value); renderOption(value, label, tagWriter); }
@Test public void bindTagWithFieldButWithoutErrorsInstance() throws JspException { PageContext pc = createPageContext(); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb.name"); pc.getRequest().setAttribute("tb", new TestBean("juergen&eva", 99)); tag.doStartTag(); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertEquals("name", status.getExpression()); assertEquals("juergen&eva", status.getValue()); }
@Test public void bindTagWithBeanButWithoutErrorsInstance() throws JspException { PageContext pc = createPageContext(); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb"); pc.getRequest().setAttribute("tb", new TestBean("juergen", 99)); tag.doStartTag(); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertNull(status.getExpression()); assertNull(status.getValue()); }
@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); }
@Test public void bindTagWithFieldButWithoutErrorsInstanceAndHtmlEscaping() throws JspException { PageContext pc = createPageContext(); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb.name"); tag.setHtmlEscape(true); pc.getRequest().setAttribute("tb", new TestBean("juergen&eva", 99)); tag.doStartTag(); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertEquals("name", status.getExpression()); assertEquals("juergen&eva", status.getValue()); }
@Test public void bindTagWithSetValueAndHtmlEscaping() throws JspException { PageContext pc = createPageContext(); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb.someSet"); tag.setHtmlEscape(true); pc.getRequest().setAttribute("tb", new TestBean("juergen&eva", 99)); tag.doStartTag(); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertEquals("someSet", status.getExpression()); assertTrue(status.getValue() instanceof Set); }
@Test public void bindErrorsTagWithErrors() throws JspException { PageContext pc = createPageContext(); Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult(); errors.reject("test", null, "test"); 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.EVAL_BODY_INCLUDE); assertTrue("Has errors variable", pc.getAttribute(BindErrorsTag.ERRORS_VARIABLE_NAME, PageContext.REQUEST_SCOPE) == errors); }
@Test public void nestedPathDoStartTagInternal() throws JspException { PageContext pc = createPageContext(); NestedPathTag tag = new NestedPathTag(); tag.setPath("foo"); tag.setPageContext(pc); int returnValue = tag.doStartTag(); assertEquals(Tag.EVAL_BODY_INCLUDE, returnValue); assertEquals("foo.", pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE)); }
@Test public void nestedPathDoEndTagWithNesting() throws JspException { PageContext pc = createPageContext(); NestedPathTag tag = new NestedPathTag(); tag.setPath("foo"); tag.setPageContext(pc); tag.doStartTag(); NestedPathTag anotherTag = new NestedPathTag(); anotherTag.setPageContext(pc); anotherTag.setPath("bar"); anotherTag.doStartTag(); anotherTag.doEndTag(); assertEquals("foo.", pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE)); tag.doEndTag(); assertNull(pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE)); }
@Test public void bindTagWithToStringAndHtmlEscaping() throws JspException { PageContext pc = createPageContext(); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb.doctor"); tag.setHtmlEscape(true); TestBean tb = new TestBean("somebody", 99); NestedTestBean ntb = new NestedTestBean("juergen&eva"); tb.setDoctor(ntb); pc.getRequest().setAttribute("tb", tb); tag.doStartTag(); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertEquals("doctor", status.getExpression()); assertTrue(status.getValue() instanceof NestedTestBean); assertTrue(status.getDisplayValue().contains("juergen&eva")); }
@Test public void nestedPathDoEndTag() throws JspException { PageContext pc = createPageContext(); NestedPathTag tag = new NestedPathTag(); tag.setPath("foo"); tag.setPageContext(pc); tag.doStartTag(); int returnValue = tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, returnValue); assertNull(pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE)); }
@Test public void nestedPathWithBindTagWithIgnoreNestedPath() throws JspException { PageContext pc = createPageContext(); Errors errors = new ServletRequestDataBinder(new TestBean(), "tb2").getBindingResult(); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb2", errors); NestedPathTag tag = new NestedPathTag(); tag.setPath("tb"); tag.setPageContext(pc); tag.doStartTag(); BindTag bindTag = new BindTag(); bindTag.setPageContext(pc); bindTag.setIgnoreNestedPath(true); bindTag.setPath("tb2.name"); assertTrue("Correct doStartTag return value", bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertTrue("Has status variable", status != null); assertEquals("tb2.name", status.getPath()); }