BindErrorsTag.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 2k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.web.servlet.tags;
  17. import javax.servlet.ServletException;
  18. import javax.servlet.jsp.JspException;
  19. import org.springframework.validation.Errors;
  20. import org.springframework.web.util.ExpressionEvaluationUtils;
  21. /**
  22.  * Evaluates content if there are bind errors for a certain bean.
  23.  * Exports an "errors" variable of type Errors for the given bean.
  24.  * @author Rod Johnson
  25.  * @author Juergen Hoeller
  26.  * @see BindTag
  27.  * @see org.springframework.validation.Errors
  28.  */
  29. public class BindErrorsTag extends HtmlEscapingAwareTag {
  30. public static final String ERRORS_VARIABLE_NAME = "errors";
  31. private String name;
  32. private Errors errors;
  33. /**
  34.  * Set the name of the bean that this tag should check.
  35.  */
  36. public void setName(String name) {
  37. this.name = name;
  38. }
  39. /**
  40.  * Return the name of the bean that this tag checks.
  41.  */
  42. public String getName() {
  43. return name;
  44. }
  45. protected final int doStartTagInternal() throws ServletException, JspException {
  46. String resolvedName = ExpressionEvaluationUtils.evaluateString("name", this.name, pageContext);
  47. this.errors = getRequestContext().getErrors(resolvedName, isHtmlEscape());
  48. if (this.errors != null && this.errors.hasErrors()) {
  49. this.pageContext.setAttribute(ERRORS_VARIABLE_NAME, this.errors);
  50. return EVAL_BODY_INCLUDE;
  51. }
  52. else {
  53. return SKIP_BODY;
  54. }
  55. }
  56. public int doEndTag() {
  57. this.pageContext.removeAttribute(ERRORS_VARIABLE_NAME);
  58. return EVAL_PAGE;
  59. }
  60. /**
  61.  * Retrieve the Errors instance that this tag is currently bound to.
  62.  * Intended for cooperating nesting tags.
  63.  */
  64. public final Errors getErrors() {
  65. return errors;
  66. }
  67. }