Search in sources :

Example 16 with BindStatus

use of org.springframework.web.servlet.support.BindStatus in project spring-framework by spring-projects.

the class OptionsTagTests method withCollectionAndCustomEditor.

@Test
public void withCollectionAndCustomEditor() 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);
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.myFloat", false));
    List<Float> floats = new ArrayList<>();
    floats.add(new Float("12.30"));
    floats.add(new Float("12.31"));
    floats.add(new Float("12.32"));
    floats.add(new Float("12.33"));
    floats.add(new Float("12.34"));
    floats.add(new Float("12.35"));
    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"));
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) BindStatus(org.springframework.web.servlet.support.BindStatus) Document(org.dom4j.Document) TestBean(org.springframework.tests.sample.beans.TestBean) StringReader(java.io.StringReader) PropertyEditor(java.beans.PropertyEditor) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 17 with BindStatus

use of org.springframework.web.servlet.support.BindStatus in project spring-framework by spring-projects.

the class OptionTagTests method withCustomObjectNotSelected.

@Test
public void withCustomObjectNotSelected() throws Exception {
    String selectName = "testBean.someNumber";
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), selectName, false));
    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");
}
Also used : BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 18 with BindStatus

use of org.springframework.web.servlet.support.BindStatus in project spring-framework by spring-projects.

the class OptionTagTests method asBodyTag.

@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);
}
Also used : MockBodyContent(org.springframework.mock.web.test.MockBodyContent) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 19 with BindStatus

use of org.springframework.web.servlet.support.BindStatus in project spring-framework by spring-projects.

the class OptionTagTests method multiBind.

@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());
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.tests.sample.beans.TestBean) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 20 with BindStatus

use of org.springframework.web.servlet.support.BindStatus in project spring-framework by spring-projects.

the class FreeMarkerMacroTests method testExposeSpringMacroHelpers.

@Test
public void testExposeSpringMacroHelpers() throws Exception {
    FreeMarkerView fv = new FreeMarkerView() {

        @Override
        @SuppressWarnings("rawtypes")
        protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response) throws TemplateException {
            Map model = fmModel.toMap();
            assertTrue(model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext);
            RequestContext rc = (RequestContext) model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
            BindStatus status = rc.getBindStatus("tb.name");
            assertEquals("name", status.getExpression());
            assertEquals("juergen", status.getValue());
        }
    };
    fv.setUrl(TEMPLATE_FILE);
    fv.setApplicationContext(wac);
    fv.setExposeSpringMacroHelpers(true);
    Map<String, Object> model = new HashMap<>();
    model.put("tb", new TestBean("juergen", 99));
    fv.render(model, request, response);
}
Also used : HashMap(java.util.HashMap) TestBean(org.springframework.tests.sample.beans.TestBean) SimpleHash(freemarker.template.SimpleHash) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) DummyMacroRequestContext(org.springframework.web.servlet.view.DummyMacroRequestContext) RequestContext(org.springframework.web.servlet.support.RequestContext) BindStatus(org.springframework.web.servlet.support.BindStatus) HashMap(java.util.HashMap) Map(java.util.Map) Template(freemarker.template.Template) Test(org.junit.Test)

Aggregations

BindStatus (org.springframework.web.servlet.support.BindStatus)47 Test (org.junit.Test)44 TestBean (org.springframework.tests.sample.beans.TestBean)22 PageContext (javax.servlet.jsp.PageContext)19 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)19 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)18 Errors (org.springframework.validation.Errors)14 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)14 PropertyEditor (java.beans.PropertyEditor)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Document (org.dom4j.Document)4 Element (org.dom4j.Element)4 SAXReader (org.dom4j.io.SAXReader)4 StringArrayPropertyEditor (org.springframework.beans.propertyeditors.StringArrayPropertyEditor)4 MockBodyContent (org.springframework.mock.web.test.MockBodyContent)3 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)2 BindTag (org.springframework.web.servlet.tags.BindTag)2 SimpleHash (freemarker.template.SimpleHash)1