BindTag.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 4k
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 java.beans.PropertyEditor;
  18. import javax.servlet.jsp.JspTagException;
  19. import javax.servlet.jsp.PageContext;
  20. import org.springframework.validation.Errors;
  21. import org.springframework.web.util.ExpressionEvaluationUtils;
  22. /**
  23.  * <p>Bind tag, supporting evaluation of binding errors for a certain
  24.  * bean or bean property. Exports a "status" variable of type BindStatus</p>
  25.  *
  26.  * <p>The errors object that has been bound using this tag is exposed, as well
  27.  * as the property that this errors object applies to. Children tags can
  28.  * use the exposed properties
  29.  *
  30.  * <p>Discussed in Chapter 12 of
  31.  * <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0764543857/">Expert One-On-One J2EE Design and Development</a>
  32.  * by Rod Johnson.
  33.  *
  34.  * @author Rod Johnson
  35.  * @author Juergen Hoeller
  36.  */
  37. public class BindTag extends HtmlEscapingAwareTag {
  38. /**
  39.  * Name of the exposed variable within the scope of this tag: "status".
  40.  */
  41. public static final String STATUS_VARIABLE_NAME = "status";
  42. private String path;
  43. private boolean ignoreNestedPath = false;
  44. private BindStatus status;
  45. /**
  46.  * Set the path that this tag should apply. Can be a bean (e.g. "person")
  47.  * to get global errors, or a bean property (e.g. "person.name") to get
  48.  * field errors (also supporting nested fields and "person.na*" mappings).
  49.  * "person.*" will return all errors for the specified bean, both global
  50.  * and field errors.
  51.  * @see org.springframework.validation.Errors#getGlobalErrors
  52.  * @see org.springframework.validation.Errors#getFieldErrors
  53.  */
  54. public void setPath(String path) {
  55. this.path = path;
  56. }
  57. /**
  58.  * Return the path that this tag applies to.
  59.  */
  60. public String getPath() {
  61. return path;
  62. }
  63. /**
  64.  * Set whether to ignore a nested path, if any.
  65.  * Default is to not ignore.
  66.  */
  67. public void setIgnoreNestedPath(boolean ignoreNestedPath) {
  68.   this.ignoreNestedPath = ignoreNestedPath;
  69. }
  70. /**
  71.  * Return whether to ignore a nested path, if any.
  72.  */
  73. public boolean isIgnoreNestedPath() {
  74.   return ignoreNestedPath;
  75. }
  76. protected final int doStartTagInternal() throws Exception {
  77. String resolvedPath = ExpressionEvaluationUtils.evaluateString("path", getPath(), pageContext);
  78. if (!isIgnoreNestedPath()) {
  79. String nestedPath = (String) this.pageContext.getAttribute(
  80. NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
  81. if (nestedPath != null) {
  82. resolvedPath = nestedPath + resolvedPath;
  83. }
  84. }
  85. try {
  86. this.status = new BindStatus(getRequestContext(), resolvedPath, isHtmlEscape());
  87. }
  88. catch (IllegalStateException ex) {
  89. throw new JspTagException(ex.getMessage());
  90. }
  91. // create the status object
  92. this.pageContext.setAttribute(STATUS_VARIABLE_NAME, this.status);
  93. return EVAL_BODY_INCLUDE;
  94. }
  95. public int doEndTag() {
  96. pageContext.removeAttribute(STATUS_VARIABLE_NAME);
  97. return EVAL_PAGE;
  98. }
  99. /**
  100.  * Retrieve the property that this tag is currently bound to,
  101.  * or null if bound to an object rather than a specific property.
  102.  * Intended for cooperating nesting tags.
  103.  * @return the property that this tag is currently bound to,
  104.  * or <code>null</code> if none
  105.  */
  106. public final String getProperty() {
  107. return this.status.getExpression();
  108. }
  109. /**
  110.  * Retrieve the Errors instance that this tag is currently bound to.
  111.  * Intended for cooperating nesting tags.
  112.  * @return the current Errors instance, or null if none
  113.  */
  114. public final Errors getErrors() {
  115. return this.status.getErrors();
  116. }
  117. /**
  118.  * Retrieve the PropertyEditor for the property that this tag is
  119.  * currently bound to. Intended for cooperating nesting tags.
  120.  * @return the current PropertyEditor, or null if none
  121.  */
  122. public final PropertyEditor getEditor() {
  123. return this.status.getEditor();
  124. }
  125. }