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"));
}
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");
}
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);
}
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());
}
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);
}
Aggregations