Skip to content

Commit 8d8766d

Browse files
committed
SelectTag correctly detects multiple="true" again
Issue: SPR-11678
1 parent 1f2d5b5 commit 8d8766d

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -249,7 +249,8 @@ private void writeHiddenTagIfNecessary(TagWriter tagWriter) throws JspException
249249

250250
private boolean isMultiple() throws JspException {
251251
Object multiple = getMultiple();
252-
if (Boolean.TRUE.equals(multiple) || "multiple".equals(multiple)) {
252+
if (multiple != null && (Boolean.TRUE.equals(multiple) ||
253+
Boolean.parseBoolean(multiple.toString()) || "multiple".equals(multiple))) {
253254
return true;
254255
}
255256
return forceMultiple();

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ public String getAsText() {
194194
transformTag.setParent(this.tag);
195195
transformTag.setPageContext(getPageContext());
196196
transformTag.doStartTag();
197-
String output = getOutput();
198-
System.err.println(output);
199197
assertEquals("Austria", getPageContext().findAttribute("key"));
200198
}
201199

@@ -255,7 +253,6 @@ public String getAsText() {
255253
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
256254
this.tag.doStartTag();
257255
String output = getOutput();
258-
System.err.println(output);
259256
assertTrue(output.startsWith("<select "));
260257
assertTrue(output.endsWith("</select>"));
261258
assertFalse(output.contains("selected=\"selected\""));
@@ -312,7 +309,6 @@ public String getAsText() {
312309
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
313310
this.tag.doStartTag();
314311
String output = getOutput();
315-
System.err.println(output);
316312
assertTrue(output.startsWith("<select "));
317313
assertTrue(output.endsWith("</select>"));
318314
assertFalse(output.contains("selected=\"selected\""));
@@ -335,8 +331,8 @@ public void testWithInvalidList() throws Exception {
335331
}
336332
catch (JspException expected) {
337333
String message = expected.getMessage();
338-
assertTrue(message.indexOf("items") > -1);
339-
assertTrue(message.indexOf("org.springframework.tests.sample.beans.TestBean") > -1);
334+
assertTrue(message.contains("items"));
335+
assertTrue(message.contains("org.springframework.tests.sample.beans.TestBean"));
340336
}
341337
}
342338

@@ -664,7 +660,6 @@ public void testWithMultiMap() throws Exception {
664660
* </ul>
665661
*/
666662
public void testWithMultiMapWithItemValueAndItemLabel() throws Exception {
667-
668663
// Save original default locale.
669664
final Locale defaultLocale = Locale.getDefault();
670665
// Use a locale that doesn't result in the generation of HTML entities
@@ -686,7 +681,6 @@ public void testWithMultiMapWithItemValueAndItemLabel() throws Exception {
686681

687682
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), COMMAND_NAME);
688683
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
689-
690684
@Override
691685
public void setAsText(final String text) throws IllegalArgumentException {
692686
setValue(Country.getCountryWithIsoCode(text));
@@ -737,7 +731,7 @@ public String getAsText() {
737731
}
738732
}
739733

740-
public void testMultiWithEmptyCollection() throws Exception {
734+
public void testMultiForCollection() throws Exception {
741735
this.bean.setSomeList(new ArrayList());
742736

743737
this.tag.setPath("someList");
@@ -766,6 +760,34 @@ public void testMultiWithEmptyCollection() throws Exception {
766760
assertNotNull(inputElement);
767761
}
768762

763+
public void testMultiExplicit() throws Exception {
764+
this.tag.setPath("name");
765+
this.tag.setItems(Country.getCountries());
766+
this.tag.setItemValue("isoCode");
767+
this.tag.setMultiple("true");
768+
int result = this.tag.doStartTag();
769+
assertEquals(Tag.SKIP_BODY, result);
770+
771+
String output = getOutput();
772+
output = "<doc>" + output + "</doc>";
773+
774+
SAXReader reader = new SAXReader();
775+
Document document = reader.read(new StringReader(output));
776+
Element rootElement = document.getRootElement();
777+
assertEquals(2, rootElement.elements().size());
778+
779+
Element selectElement = rootElement.element("select");
780+
assertEquals("select", selectElement.getName());
781+
assertEquals("name", selectElement.attribute("name").getValue());
782+
assertEquals("multiple", selectElement.attribute("multiple").getValue());
783+
784+
List children = selectElement.elements();
785+
assertEquals("Incorrect number of children", 4, children.size());
786+
787+
Element inputElement = rootElement.element("input");
788+
assertNotNull(inputElement);
789+
}
790+
769791

770792
private void assertStringArray() throws JspException, DocumentException {
771793
int result = this.tag.doStartTag();

0 commit comments

Comments
 (0)