KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > tags > NestedPathTag


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.web.servlet.tags;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.PageContext JavaDoc;
21 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
22
23 import org.springframework.validation.Errors;
24 import org.springframework.web.util.ExpressionEvaluationUtils;
25
26 /**
27  * <p>Nested-path tag, to support and assist with nested beans or bean properties
28  * in the model. Exports a "nestedPath" variable of type String in request scope,
29  * visible to the current page and also included pages, if any.
30  *
31  * <p>The BindTag will auto-detect the current nested path and automatically
32  * prepend it to its own path to form a complete path to the bean or bean property.
33  *
34  * <p>This tag will also prepend any existing nested path that is currently set.
35  * Thus, you can nest multiple nested-path tags.
36  *
37  * <p>Thanks to Seth Ladd for the suggestion and the original implementation!
38  *
39  * @author Juergen Hoeller
40  * @since 1.1
41  */

42 public class NestedPathTag extends TagSupport JavaDoc {
43
44     /**
45      * Name of the exposed variable within the scope of this tag: "nestedPath".
46      */

47     public static final String JavaDoc NESTED_PATH_VARIABLE_NAME = "nestedPath";
48
49
50     private String JavaDoc path;
51
52     /** To cache any previous nested path, so that it may be reset */
53     private String JavaDoc previousNestedPath = "";
54
55
56     /**
57      * Set the path that this tag should apply.
58      * <p>E.g. "customer" to allow bind paths like "address.street"
59      * rather than "customer.address.street".
60      * @see BindTag#setPath
61      */

62     public void setPath(String JavaDoc path) {
63         if (path == null) {
64             path = "";
65         }
66         if (path.length() > 0 && !path.endsWith(Errors.NESTED_PATH_SEPARATOR)) {
67             path += Errors.NESTED_PATH_SEPARATOR;
68         }
69         this.path = path;
70     }
71
72     /**
73      * Return the path that this tag applies to.
74      */

75     public String JavaDoc getPath() {
76         return path;
77     }
78
79
80     public int doStartTag() throws JspException JavaDoc {
81         String JavaDoc resolvedPath = ExpressionEvaluationUtils.evaluateString("path", getPath(), pageContext);
82
83         // Save previous nestedPath value, build and expose current nestedPath value.
84
// Use request scope to expose nestedPath to included pages too.
85
String JavaDoc nestedPath = (String JavaDoc) pageContext.getAttribute(NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
86         if (nestedPath != null) {
87             this.previousNestedPath = nestedPath;
88             nestedPath = nestedPath + resolvedPath;
89         }
90         else {
91             nestedPath = resolvedPath;
92         }
93         pageContext.setAttribute(NESTED_PATH_VARIABLE_NAME, nestedPath, PageContext.REQUEST_SCOPE);
94
95         return EVAL_BODY_INCLUDE;
96     }
97
98     /**
99      * Reset any previous nestedPath value.
100      */

101     public int doEndTag() {
102         if (this.previousNestedPath != null) {
103             // Expose previous nestedPath value.
104
pageContext.setAttribute(NESTED_PATH_VARIABLE_NAME, this.previousNestedPath, PageContext.REQUEST_SCOPE);
105         }
106         else {
107             // Remove exposed nestedPath value.
108
pageContext.removeAttribute(NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
109         }
110
111         return EVAL_PAGE;
112     }
113
114 }
115
Popular Tags