From 8e090e5fc4a227b3f64f92eab1d7b5b3f44c94e3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 7 Jun 2010 19:22:53 +0000 Subject: [PATCH] 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 --- .../web/servlet/tags/form/ErrorsTag.java | 2 +- .../web/servlet/tags/form/ErrorsTagTests.java | 38 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/ErrorsTag.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/ErrorsTag.java index ccc1fc160f4..2a8d0f5ba51 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/ErrorsTag.java +++ b/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 { @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); } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/ErrorsTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/ErrorsTagTests.java index ccea7230e93..d84f0f59152 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/ErrorsTagTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/form/ErrorsTagTests.java @@ -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) {