NestedPathTag.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 3k
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.jsp.JspException;
  18. import javax.servlet.jsp.PageContext;
  19. import javax.servlet.jsp.tagext.TagSupport;
  20. import org.springframework.validation.Errors;
  21. import org.springframework.web.util.ExpressionEvaluationUtils;
  22. /**
  23.  * <p>Nested path tag, to support and assist with nested beans or bean properties.
  24.  * Exports a "nestedPath" variable of type String.
  25.  *
  26.  * <p>The BindTag will auto-detect the nested path and automatically prepend it
  27.  * to its own path to form a complete path to the bean or bean property.
  28.  *
  29.  * <p>This tag will also prepend any existing nested path that is currently set.
  30.  * Thus, you can nested multiple nested path tags.
  31.  *
  32.  * @author Seth Ladd
  33.  * @author Juergen Hoeller
  34.  * @since 28.07.2004
  35.  */
  36. public class NestedPathTag extends TagSupport {
  37. /**
  38.  * Name of the exposed variable within the scope of this tag: "nestedPath".
  39.  */
  40. public static final String NESTED_PATH_VARIABLE_NAME = "nestedPath";
  41. private String path;
  42. /** To cache any previous nested path, so that it may be reset */
  43. private String previousNestedPath = "";
  44. /**
  45.  * Set the path that this tag should apply.
  46.  * <p>E.g. "customer" to allow bind paths like "address.street"
  47.  * rather than "customer.address.street".
  48.  * @see BindTag#setPath
  49.  */
  50. public void setPath(String path) {
  51. if (path == null) {
  52. path = "";
  53. }
  54. if (path.length() > 0 && !path.endsWith(Errors.NESTED_PATH_SEPARATOR)) {
  55. path += Errors.NESTED_PATH_SEPARATOR;
  56. }
  57. this.path = path;
  58. }
  59. /**
  60.  * Return the path that this tag applies to.
  61.  */
  62. public String getPath() {
  63. return path;
  64. }
  65. public int doStartTag() throws JspException {
  66. String resolvedPath = ExpressionEvaluationUtils.evaluateString("path", getPath(), pageContext);
  67. String nestedPath = (String) pageContext.getAttribute(NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
  68. if (nestedPath != null) {
  69. this.previousNestedPath = nestedPath;
  70. nestedPath = nestedPath + resolvedPath;
  71. }
  72. else {
  73. nestedPath = resolvedPath;
  74. }
  75. this.pageContext.setAttribute(NESTED_PATH_VARIABLE_NAME, nestedPath, PageContext.REQUEST_SCOPE);
  76. return EVAL_BODY_INCLUDE;
  77. }
  78. /**
  79.  * Reset any previous nestedPath value.
  80.  */
  81. public int doEndTag() {
  82. if (this.previousNestedPath != null) {
  83. pageContext.setAttribute(NESTED_PATH_VARIABLE_NAME, this.previousNestedPath, PageContext.REQUEST_SCOPE);
  84. }
  85. else {
  86. pageContext.removeAttribute(NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
  87. }
  88. return EVAL_PAGE;
  89. }
  90. }