fixed JSP ErrorsTag to avoid invalid "*.errors" id, using form object name as id prefix instead (SPR-7258)
This commit is contained in:
@@ -112,7 +112,7 @@ public class ErrorsTag extends AbstractHtmlElementBodyTag implements BodyTag {
|
||||
@Override
|
||||
protected String autogenerateId() throws JspException {
|
||||
String path = getPropertyPath();
|
||||
if ("".equals(path)) {
|
||||
if ("".equals(path) || "*".equals(path)) {
|
||||
path = (String) this.pageContext.getAttribute(
|
||||
FormTag.MODEL_ATTRIBUTE_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,6 @@ package org.springframework.web.servlet.tags.form;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.PageContext;
|
||||
@@ -399,8 +398,39 @@ public class ErrorsTagTests extends AbstractFormTagTests {
|
||||
assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
|
||||
this.tag.doEndTag();
|
||||
String output = getOutput();
|
||||
assertBlockTagContains(output, "object error");
|
||||
assertFalse(output.indexOf("field error") != -1);
|
||||
assertTrue(output.contains("id=\"testBean.errors\""));
|
||||
assertTrue(output.contains("object error"));
|
||||
assertFalse(output.contains("field error"));
|
||||
}
|
||||
|
||||
public void testSpecificPathMatchesSpecificFieldOnly() throws Exception {
|
||||
this.tag.setPath("name");
|
||||
Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME");
|
||||
errors.reject("some.code", "object error");
|
||||
errors.rejectValue("name", "some.code", "field error");
|
||||
exposeBindingResult(errors);
|
||||
this.tag.doStartTag();
|
||||
assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
|
||||
this.tag.doEndTag();
|
||||
String output = getOutput();
|
||||
assertTrue(output.contains("id=\"name.errors\""));
|
||||
assertFalse(output.contains("object error"));
|
||||
assertTrue(output.contains("field error"));
|
||||
}
|
||||
|
||||
public void testStarMatchesAllErrors() throws Exception {
|
||||
this.tag.setPath("*");
|
||||
Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME");
|
||||
errors.reject("some.code", "object error");
|
||||
errors.rejectValue("name", "some.code", "field error");
|
||||
exposeBindingResult(errors);
|
||||
this.tag.doStartTag();
|
||||
assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
|
||||
this.tag.doEndTag();
|
||||
String output = getOutput();
|
||||
assertTrue(output.contains("id=\"testBean.errors\""));
|
||||
assertTrue(output.contains("object error"));
|
||||
assertTrue(output.contains("field error"));
|
||||
}
|
||||
|
||||
protected void exposeBindingResult(Errors errors) {
|
||||
|
||||
Reference in New Issue
Block a user