Browse Source

SelectTag consistently checks specified 'multiple' attribute now, never falling back to forceMultiple in case of user-provided value

Issue: SPR-11903
pull/568/head
Juergen Hoeller 11 years ago
parent
commit
779ca99f38
  1. 8
      spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/SelectTag.java
  2. 111
      spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/SelectTagTests.java

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

@ -84,7 +84,7 @@ public class SelectTag extends AbstractHtmlInputElementTag { @@ -84,7 +84,7 @@ public class SelectTag extends AbstractHtmlInputElementTag {
* Indicates whether or not the '{@code select}' tag allows
* multiple-selections.
*/
private Object multiple = Boolean.FALSE;
private Object multiple;
/**
* The {@link TagWriter} instance that the output is being written.
@ -249,9 +249,9 @@ public class SelectTag extends AbstractHtmlInputElementTag { @@ -249,9 +249,9 @@ public class SelectTag extends AbstractHtmlInputElementTag {
private boolean isMultiple() throws JspException {
Object multiple = getMultiple();
if (multiple != null && (Boolean.TRUE.equals(multiple) ||
Boolean.parseBoolean(multiple.toString()) || "multiple".equals(multiple))) {
return true;
if (multiple != null) {
String stringValue = multiple.toString();
return ("multiple".equalsIgnoreCase(stringValue) || Boolean.parseBoolean(stringValue));
}
return forceMultiple();
}

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

@ -256,6 +256,7 @@ public class SelectTagTests extends AbstractFormTagTests { @@ -256,6 +256,7 @@ public class SelectTagTests extends AbstractFormTagTests {
assertTrue(output.startsWith("<select "));
assertTrue(output.endsWith("</select>"));
assertFalse(output.contains("selected=\"selected\""));
assertFalse(output.contains("multiple=\"multiple\""));
}
public void testNestedPathWithListAndEditor() throws Exception {
@ -731,7 +732,7 @@ public class SelectTagTests extends AbstractFormTagTests { @@ -731,7 +732,7 @@ public class SelectTagTests extends AbstractFormTagTests {
}
}
public void testMultiForCollection() throws Exception {
public void testMultipleForCollection() throws Exception {
this.bean.setSomeList(new ArrayList());
this.tag.setPath("someList");
@ -760,7 +761,35 @@ public class SelectTagTests extends AbstractFormTagTests { @@ -760,7 +761,35 @@ public class SelectTagTests extends AbstractFormTagTests {
assertNotNull(inputElement);
}
public void testMultiExplicit() throws Exception {
public void testMultipleWithStringValue() throws Exception {
this.tag.setPath("name");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
this.tag.setMultiple("multiple");
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);
}
public void testMultipleExplicitlyTrue() throws Exception {
this.tag.setPath("name");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
@ -788,6 +817,84 @@ public class SelectTagTests extends AbstractFormTagTests { @@ -788,6 +817,84 @@ public class SelectTagTests extends AbstractFormTagTests {
assertNotNull(inputElement);
}
public void testMultipleExplicitlyFalse() throws Exception {
this.tag.setPath("name");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
this.tag.setMultiple("false");
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(1, rootElement.elements().size());
Element selectElement = rootElement.element("select");
assertEquals("select", selectElement.getName());
assertEquals("name", selectElement.attribute("name").getValue());
assertNull(selectElement.attribute("multiple"));
List children = selectElement.elements();
assertEquals("Incorrect number of children", 4, children.size());
}
public void testMultipleWithBooleanTrue() 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);
}
public void testMultipleWithBooleanFalse() throws Exception {
this.tag.setPath("name");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
this.tag.setMultiple(false);
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(1, rootElement.elements().size());
Element selectElement = rootElement.element("select");
assertEquals("select", selectElement.getName());
assertEquals("name", selectElement.attribute("name").getValue());
assertNull(selectElement.attribute("multiple"));
List children = selectElement.elements();
assertEquals("Incorrect number of children", 4, children.size());
}
private void assertStringArray() throws JspException, DocumentException {
int result = this.tag.doStartTag();

Loading…
Cancel
Save