Browse Source

SelectTag correctly detects multiple="true" again

Issue: SPR-11678
pull/512/head
Juergen Hoeller 12 years ago
parent
commit
8d8766dda9
  1. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/SelectTag.java
  2. 40
      spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/SelectTagTests.java

5
spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/SelectTag.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -249,7 +249,8 @@ public class SelectTag extends AbstractHtmlInputElementTag {
private boolean isMultiple() throws JspException { private boolean isMultiple() throws JspException {
Object multiple = getMultiple(); Object multiple = getMultiple();
if (Boolean.TRUE.equals(multiple) || "multiple".equals(multiple)) { if (multiple != null && (Boolean.TRUE.equals(multiple) ||
Boolean.parseBoolean(multiple.toString()) || "multiple".equals(multiple))) {
return true; return true;
} }
return forceMultiple(); return forceMultiple();

40
spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/SelectTagTests.java

@ -194,8 +194,6 @@ public class SelectTagTests extends AbstractFormTagTests {
transformTag.setParent(this.tag); transformTag.setParent(this.tag);
transformTag.setPageContext(getPageContext()); transformTag.setPageContext(getPageContext());
transformTag.doStartTag(); transformTag.doStartTag();
String output = getOutput();
System.err.println(output);
assertEquals("Austria", getPageContext().findAttribute("key")); assertEquals("Austria", getPageContext().findAttribute("key"));
} }
@ -255,7 +253,6 @@ public class SelectTagTests extends AbstractFormTagTests {
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag(); this.tag.doStartTag();
String output = getOutput(); String output = getOutput();
System.err.println(output);
assertTrue(output.startsWith("<select ")); assertTrue(output.startsWith("<select "));
assertTrue(output.endsWith("</select>")); assertTrue(output.endsWith("</select>"));
assertFalse(output.contains("selected=\"selected\"")); assertFalse(output.contains("selected=\"selected\""));
@ -312,7 +309,6 @@ public class SelectTagTests extends AbstractFormTagTests {
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag(); this.tag.doStartTag();
String output = getOutput(); String output = getOutput();
System.err.println(output);
assertTrue(output.startsWith("<select ")); assertTrue(output.startsWith("<select "));
assertTrue(output.endsWith("</select>")); assertTrue(output.endsWith("</select>"));
assertFalse(output.contains("selected=\"selected\"")); assertFalse(output.contains("selected=\"selected\""));
@ -335,8 +331,8 @@ public class SelectTagTests extends AbstractFormTagTests {
} }
catch (JspException expected) { catch (JspException expected) {
String message = expected.getMessage(); String message = expected.getMessage();
assertTrue(message.indexOf("items") > -1); assertTrue(message.contains("items"));
assertTrue(message.indexOf("org.springframework.tests.sample.beans.TestBean") > -1); assertTrue(message.contains("org.springframework.tests.sample.beans.TestBean"));
} }
} }
@ -664,7 +660,6 @@ public class SelectTagTests extends AbstractFormTagTests {
* </ul> * </ul>
*/ */
public void testWithMultiMapWithItemValueAndItemLabel() throws Exception { public void testWithMultiMapWithItemValueAndItemLabel() throws Exception {
// Save original default locale. // Save original default locale.
final Locale defaultLocale = Locale.getDefault(); final Locale defaultLocale = Locale.getDefault();
// Use a locale that doesn't result in the generation of HTML entities // Use a locale that doesn't result in the generation of HTML entities
@ -686,7 +681,6 @@ public class SelectTagTests extends AbstractFormTagTests {
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), COMMAND_NAME); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), COMMAND_NAME);
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() { bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
@Override @Override
public void setAsText(final String text) throws IllegalArgumentException { public void setAsText(final String text) throws IllegalArgumentException {
setValue(Country.getCountryWithIsoCode(text)); setValue(Country.getCountryWithIsoCode(text));
@ -737,7 +731,7 @@ public class SelectTagTests extends AbstractFormTagTests {
} }
} }
public void testMultiWithEmptyCollection() throws Exception { public void testMultiForCollection() throws Exception {
this.bean.setSomeList(new ArrayList()); this.bean.setSomeList(new ArrayList());
this.tag.setPath("someList"); this.tag.setPath("someList");
@ -766,6 +760,34 @@ public class SelectTagTests extends AbstractFormTagTests {
assertNotNull(inputElement); assertNotNull(inputElement);
} }
public void testMultiExplicit() throws Exception {
this.tag.setPath("name");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
this.tag.setMultiple("true");
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();
assertEquals(2, rootElement.elements().size());
Element selectElement = rootElement.element("select");
assertEquals("select", selectElement.getName());
assertEquals("name", selectElement.attribute("name").getValue());
assertEquals("multiple", selectElement.attribute("multiple").getValue());
List children = selectElement.elements();
assertEquals("Incorrect number of children", 4, children.size());
Element inputElement = rootElement.element("input");
assertNotNull(inputElement);
}
private void assertStringArray() throws JspException, DocumentException { private void assertStringArray() throws JspException, DocumentException {
int result = this.tag.doStartTag(); int result = this.tag.doStartTag();

Loading…
Cancel
Save