Example usage for org.springframework.web.servlet.support BindStatus BindStatus

List of usage examples for org.springframework.web.servlet.support BindStatus BindStatus

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support BindStatus BindStatus.

Prototype

public BindStatus(RequestContext requestContext, String path, boolean htmlEscape) throws IllegalStateException 

Source Link

Document

Create a new BindStatus instance, representing a field or object status.

Usage

From source file:org.hdiv.web.servlet.tags.form.OptionsTagTests.java

public void testWithCollection() throws Exception {
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE,
            new BindStatus(getRequestContext(), "testBean.country", false));

    this.tag.setItems("${countries}");
    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();//  w  w w .j a  va2 s.c  om
    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());

    String hdivValue = this.confidentiality ? "2" : "UK";

    Element element = (Element) rootElement.selectSingleNode("option[@value = '" + hdivValue + "']");
    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());
}

From source file:org.openmrs.web.taglib.FormatDateTag.java

public int doStartTag() {
    RequestContext requestContext = (RequestContext) this.pageContext
            .getAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE);

    if (date == null && getPath() != null) {
        try {/* ww  w.  j  av  a 2  s.com*/
            // get the "path" object from the pageContext
            String resolvedPath = getPath();
            String nestedPath = (String) pageContext.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME,
                    PageContext.REQUEST_SCOPE);
            if (nestedPath != null) {
                resolvedPath = nestedPath + resolvedPath;
            }

            BindStatus status = new BindStatus(requestContext, resolvedPath, false);
            log.debug("status: " + status);

            if (status.getValue() != null) {
                log.debug("status.value: " + status.getValue());
                if (status.getValue().getClass() == Date.class) {
                    // if no editor was registered all will go well here
                    date = (Date) status.getValue();
                } else {
                    // if a "Date" property editor was registerd for the form, the status.getValue()
                    // object will be a java.lang.String.  This is useless.  Try getting the original
                    // value from the troublesome editor
                    log.debug("status.valueType: " + status.getValueType());
                    Timestamp timestamp = (Timestamp) status.getEditor().getValue();
                    date = new Date(timestamp.getTime());
                }
            }
        } catch (Exception e) {
            log.warn("Unable to get a date object from path: " + getPath(), e);
            return SKIP_BODY;
        }
    }

    if (!dateWasSet && date == null) {
        log.warn("Both 'date' and 'path' cannot be null.  Page: " + pageContext.getPage() + " localname:"
                + pageContext.getRequest().getLocalName() + " rd:"
                + pageContext.getRequest().getRequestDispatcher(""));
        return SKIP_BODY;
    }

    if (type == null) {
        type = "";
    }

    DateFormat dateFormat = null;

    if (format != null && format.length() > 0) {
        dateFormat = new SimpleDateFormat(format, Context.getLocale());
    } else if (type.equals("xml")) {
        dateFormat = new SimpleDateFormat("dd-MMM-yyyy", Context.getLocale());
    } else {
        log.debug("context locale: " + Context.getLocale());

        if (type.equals("long")) {
            dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Context.getLocale());
        } else if (type.equals("medium")) {
            dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Context.getLocale());
        } else {
            dateFormat = Context.getDateFormat();
        }
    }

    if (dateFormat == null) {
        dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    }

    String datestr = "";

    try {
        if (date != null) {
            if (type.equals("milliseconds")) {
                datestr = "" + date.getTime();
            } else {
                if (showTodayOrYesterday && (DateUtils.isSameDay(Calendar.getInstance().getTime(), date)
                        || OpenmrsUtil.isYesterday(date))) {
                    //print only time of day but maintaining the format(24 Vs 12) if any was specified
                    String timeFormatString = (format != null && !format.contains("a")) ? "HH:mm" : "h:mm a";
                    dateFormat = new SimpleDateFormat(timeFormatString);
                    if (DateUtils.isSameDay(Calendar.getInstance().getTime(), date)) {
                        datestr = Context.getMessageSourceService().getMessage("general.today") + " "
                                + dateFormat.format(date);
                    } else {
                        datestr = Context.getMessageSourceService().getMessage("general.yesterday") + " "
                                + dateFormat.format(date);
                    }
                } else {
                    datestr = dateFormat.format(date);
                }
            }
        }
    } catch (IllegalArgumentException e) {
        //format or date is invalid
        log.error("date: " + date);
        log.error("format: " + format);
        log.error(e);
        datestr = date.toString();
    }

    try {
        pageContext.getOut().write(datestr);
    } catch (IOException e) {
        log.error(e);
    }

    // reset the objects to null because taglibs are reused
    release();

    return SKIP_BODY;
}

From source file:org.hdiv.web.servlet.tags.form.OptionTagTests.java

public void testCanBeDisabledEvenWhenSelected() throws Exception {
    getPageContext().setAttribute(SelectTagHDIV.LIST_VALUE_PAGE_ATTRIBUTE,
            new BindStatus(getRequestContext(), "testBean.name", false));
    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();/*from  w w w.  j  a  v a2  s. c  o m*/

    assertOptionTagOpened(output);
    assertOptionTagClosed(output);

    String hdivValue = this.confidentiality ? "0" : "bar";
    assertContainsAttribute(output, "value", hdivValue);
    assertContainsAttribute(output, "disabled", "disabled");
    assertBlockTagContains(output, "Bar");
}

From source file:org.hdiv.web.servlet.tags.form.OptionTagTests.java

public void testRenderNotSelected() throws Exception {
    getPageContext().setAttribute(SelectTagHDIV.LIST_VALUE_PAGE_ATTRIBUTE,
            new BindStatus(getRequestContext(), "testBean.name", false));
    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();/*ww w  . j  av  a  2 s  . co m*/

    assertOptionTagOpened(output);
    assertOptionTagClosed(output);

    String hdivValue = this.confidentiality ? "0" : "bar";
    assertContainsAttribute(output, "value", hdivValue);
    assertBlockTagContains(output, "Bar");
}

From source file:com.trenako.web.tags.LocalizedTextAreaTags.java

protected BindStatus getBindStatus() throws JspException {
    String resolvedPath = ExpressionEvaluationUtils.evaluateString("path", getPath(), pageContext);
    String nestedPath = (String) pageContext.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME,
            PageContext.REQUEST_SCOPE);//from   ww w  .  j  av a2  s . c  om

    if (nestedPath != null && !resolvedPath.startsWith(nestedPath)
            && !resolvedPath.equals(nestedPath.substring(0, nestedPath.length() - 1))) {
        resolvedPath = nestedPath + resolvedPath;
    }

    return new BindStatus(getRequestContext(), resolvedPath, isHtmlEscape());
}

From source file:org.hdiv.web.servlet.view.DummyMacroRequestContext.java

/**
 * @see org.springframework.web.servlet.support.RequestContext#getBindStatus(String)
 *///from  ww w .  j a v  a 2 s.  co m
public BindStatus getBindStatus(String path) throws IllegalStateException {
    return new BindStatus(new RequestContext(this.request), path, false);
}

From source file:org.hdiv.web.servlet.view.DummyMacroRequestContext.java

/**
 * @see org.springframework.web.servlet.support.RequestContext#getBindStatus(String, boolean)
 */// w  ww.j ava2 s .  co m
public BindStatus getBindStatus(String path, boolean htmlEscape) throws IllegalStateException {
    return new BindStatus(new RequestContext(this.request), path, true);
}

From source file:org.hdiv.web.servlet.tags.form.OptionsTagTests.java

public void testWithCollectionAndCustomEditor() throws Exception {
    PropertyEditor propertyEditor = new SimpleFloatEditor();

    TestBean target = new TestBean();
    target.setMyFloat(new Float("12.34"));

    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
    errors.getPropertyAccessor().registerCustomEditor(Float.class, propertyEditor);
    exposeBindingResult(errors);/*from w  ww  .  j ava 2s  . co  m*/

    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE,
            new BindStatus(getRequestContext(), "testBean.myFloat", false));

    this.tag.setItems("${floats}");
    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", 6, children.size());

    Element element = (Element) rootElement.selectSingleNode("option[text() = '12.34f']");
    assertNotNull("Option node should not be null", element);
    assertEquals("12.34 node not selected", "selected", element.attribute("selected").getValue());
    assertNull("No id rendered", element.attribute("id"));

    element = (Element) rootElement.selectSingleNode("option[text() = '12.35f']");
    assertNotNull("Option node should not be null", element);
    assertNull("12.35 node incorrectly selected", element.attribute("selected"));
    assertNull("No id rendered", element.attribute("id"));
}

From source file:org.hdiv.web.servlet.tags.form.OptionTagTests.java

public void testRenderSelected() throws Exception {
    getPageContext().setAttribute(SelectTagHDIV.LIST_VALUE_PAGE_ATTRIBUTE,
            new BindStatus(getRequestContext(), "testBean.name", false));
    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();// ww  w .  ja v a  2 s . c  om

    assertOptionTagOpened(output);
    assertOptionTagClosed(output);

    String hdivValue = this.confidentiality ? "0" : "foo";
    assertContainsAttribute(output, "value", hdivValue);
    assertContainsAttribute(output, "selected", "selected");
    assertBlockTagContains(output, "Foo");
}

From source file:org.hdiv.web.servlet.tags.form.OptionTagTests.java

public void testWithNoLabel() throws Exception {
    getPageContext().setAttribute(SelectTagHDIV.LIST_VALUE_PAGE_ATTRIBUTE,
            new BindStatus(getRequestContext(), "testBean.name", false));
    this.tag.setValue("bar");
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);

    String output = getOutput();//ww w.  j  a v a 2  s  .c o  m

    assertOptionTagOpened(output);
    assertOptionTagClosed(output);

    String hdivValue = this.confidentiality ? "0" : "bar";
    assertContainsAttribute(output, "value", hdivValue);
    assertBlockTagContains(output, "bar");
}