Skip to content

Commit 53cdb9d

Browse files
committed
fixed JSP ErrorsTag to avoid invalid "*.errors" id, using form object name as id prefix instead (SPR-7258)
1 parent cc23820 commit 53cdb9d

File tree

2 files changed

+35
-5
lines changed
  • org.springframework.web.servlet/src

2 files changed

+35
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public String getDelimiter() {
112112
@Override
113113
protected String autogenerateId() throws JspException {
114114
String path = getPropertyPath();
115-
if ("".equals(path)) {
115+
if ("".equals(path) || "*".equals(path)) {
116116
path = (String) this.pageContext.getAttribute(
117117
FormTag.MODEL_ATTRIBUTE_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
118118
}

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

Lines changed: 34 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.
@@ -19,7 +19,6 @@
1919
import java.util.HashMap;
2020
import java.util.List;
2121
import java.util.Map;
22-
2322
import javax.servlet.http.HttpServletRequest;
2423
import javax.servlet.jsp.JspException;
2524
import javax.servlet.jsp.PageContext;
@@ -399,8 +398,39 @@ public void testOmittedPathMatchesObjectErrorsOnly() throws Exception {
399398
assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
400399
this.tag.doEndTag();
401400
String output = getOutput();
402-
assertBlockTagContains(output, "object error");
403-
assertFalse(output.indexOf("field error") != -1);
401+
assertTrue(output.contains("id=\"testBean.errors\""));
402+
assertTrue(output.contains("object error"));
403+
assertFalse(output.contains("field error"));
404+
}
405+
406+
public void testSpecificPathMatchesSpecificFieldOnly() throws Exception {
407+
this.tag.setPath("name");
408+
Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME");
409+
errors.reject("some.code", "object error");
410+
errors.rejectValue("name", "some.code", "field error");
411+
exposeBindingResult(errors);
412+
this.tag.doStartTag();
413+
assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
414+
this.tag.doEndTag();
415+
String output = getOutput();
416+
assertTrue(output.contains("id=\"name.errors\""));
417+
assertFalse(output.contains("object error"));
418+
assertTrue(output.contains("field error"));
419+
}
420+
421+
public void testStarMatchesAllErrors() throws Exception {
422+
this.tag.setPath("*");
423+
Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME");
424+
errors.reject("some.code", "object error");
425+
errors.rejectValue("name", "some.code", "field error");
426+
exposeBindingResult(errors);
427+
this.tag.doStartTag();
428+
assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
429+
this.tag.doEndTag();
430+
String output = getOutput();
431+
assertTrue(output.contains("id=\"testBean.errors\""));
432+
assertTrue(output.contains("object error"));
433+
assertTrue(output.contains("field error"));
404434
}
405435

406436
protected void exposeBindingResult(Errors errors) {

0 commit comments

Comments
 (0)