Browse Source

fixed JSP ErrorsTag to avoid invalid "*.errors" id, using form object name as id prefix instead (SPR-7258)

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3384 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Juergen Hoeller 16 years ago
parent
commit
8e090e5fc4
  1. 2
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/ErrorsTag.java
  2. 38
      org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/ErrorsTagTests.java

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

@ -112,7 +112,7 @@ public class ErrorsTag extends AbstractHtmlElementBodyTag implements BodyTag { @@ -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);
}

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

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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) {

Loading…
Cancel
Save