Skip to content

Commit a6af91f

Browse files
committed
avoid rendering invalid ids (SPR-6840)
1 parent e8a4ddd commit a6af91f

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/AbstractDataBoundFormElementTag.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2010 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.
@@ -17,7 +17,6 @@
1717
package org.springframework.web.servlet.tags.form;
1818

1919
import java.beans.PropertyEditor;
20-
2120
import javax.servlet.jsp.JspException;
2221
import javax.servlet.jsp.PageContext;
2322

@@ -145,7 +144,7 @@ protected String resolveId() throws JspException {
145144
* deleting invalid characters (such as "[" or "]").
146145
*/
147146
protected String autogenerateId() throws JspException {
148-
return StringUtils.deleteAny(getName(), "[]");
147+
return getName();
149148
}
150149

151150
/**
@@ -158,7 +157,7 @@ protected String autogenerateId() throws JspException {
158157
* @return the value for the HTML '<code>name</code>' attribute
159158
*/
160159
protected String getName() throws JspException {
161-
return getPropertyPath();
160+
return StringUtils.deleteAny(getPropertyPath(), "[]");
162161
}
163162

164163
/**

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/CheckboxTagTests.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2010 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.
@@ -21,8 +21,9 @@
2121
import java.util.ArrayList;
2222
import java.util.Calendar;
2323
import java.util.Date;
24+
import java.util.LinkedHashMap;
2425
import java.util.List;
25-
26+
import java.util.Map;
2627
import javax.servlet.jsp.tagext.Tag;
2728

2829
import org.dom4j.Document;
@@ -72,11 +73,34 @@ public void testWithSingleValueBooleanObjectChecked() throws Exception {
7273
Element checkboxElement = (Element) rootElement.elements().get(0);
7374
assertEquals("input", checkboxElement.getName());
7475
assertEquals("checkbox", checkboxElement.attribute("type").getValue());
76+
assertEquals("someBoolean1", checkboxElement.attribute("id").getValue());
7577
assertEquals("someBoolean", checkboxElement.attribute("name").getValue());
7678
assertEquals("checked", checkboxElement.attribute("checked").getValue());
7779
assertEquals("true", checkboxElement.attribute("value").getValue());
7880
}
7981

82+
public void testWithIndexedBooleanObjectNotChecked() throws Exception {
83+
this.tag.setPath("someMap[key]");
84+
int result = this.tag.doStartTag();
85+
assertEquals(Tag.SKIP_BODY, result);
86+
String output = getOutput();
87+
88+
// wrap the output so it is valid XML
89+
output = "<doc>" + output + "</doc>";
90+
91+
SAXReader reader = new SAXReader();
92+
Document document = reader.read(new StringReader(output));
93+
Element rootElement = document.getRootElement();
94+
assertEquals("Both tag and hidden element not rendered", 2, rootElement.elements().size());
95+
Element checkboxElement = (Element) rootElement.elements().get(0);
96+
assertEquals("input", checkboxElement.getName());
97+
assertEquals("checkbox", checkboxElement.attribute("type").getValue());
98+
assertEquals("someMapkey1", checkboxElement.attribute("id").getValue());
99+
assertEquals("someMapkey", checkboxElement.attribute("name").getValue());
100+
assertEquals("checked", checkboxElement.attribute("checked").getValue());
101+
assertEquals("true", checkboxElement.attribute("value").getValue());
102+
}
103+
80104
public void testWithSingleValueBooleanObjectCheckedAndDynamicAttributes() throws Exception {
81105
String dynamicAttribute1 = "attr1";
82106
String dynamicAttribute2 = "attr2";
@@ -609,6 +633,13 @@ protected TestBean createTestBean() {
609633
pets.add(new Pet("Fluffy"));
610634
pets.add(new Pet("Mufty"));
611635

636+
List someList = new ArrayList();
637+
someList.add("foo");
638+
someList.add("bar");
639+
640+
Map someMap = new LinkedHashMap();
641+
someMap.put("key", Boolean.TRUE);
642+
612643
this.bean = new TestBean();
613644
this.bean.setDate(getDate());
614645
this.bean.setName("Rob Harrop");
@@ -618,10 +649,8 @@ protected TestBean createTestBean() {
618649
this.bean.setSomeIntegerArray(new Integer[] {new Integer(2), new Integer(1)});
619650
this.bean.setOtherColours(colours);
620651
this.bean.setPets(pets);
621-
List list = new ArrayList();
622-
list.add("foo");
623-
list.add("bar");
624-
this.bean.setSomeList(list);
652+
this.bean.setSomeList(someList);
653+
this.bean.setSomeMap(someMap);
625654
return this.bean;
626655
}
627656

0 commit comments

Comments
 (0)