KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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 java.beans.PropertyEditor JavaDoc;
20
21 import javax.servlet.jsp.JspTagException JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23
24 import org.springframework.validation.Errors;
25 import org.springframework.web.servlet.support.BindStatus;
26 import org.springframework.web.util.ExpressionEvaluationUtils;
27
28 /**
29  * Bind tag, supporting evaluation of binding errors for a certain
30  * bean or bean property. Exposes a "status" variable of type
31  * {@link org.springframework.web.servlet.support.BindStatus},
32  * to both Java expressions and JSP EL expressions.
33  *
34  * <p>Can be used to bind to any bean or bean property in the model.
35  * The specified path determines whether the tag exposes the status of the
36  * bean itself (showing object-level errors), a specific bean property
37  * (showing field errors), or a matching set of bean properties
38  * (showing all corresponding field errors).
39  *
40  * <p>The {@link org.springframework.validation.Errors} object that has
41  * been bound using this tag is exposed to collaborating tags, as well
42  * as the bean property that this errors object applies to. Nested tags
43  * such as the {@link TransformTag} can access those exposed properties.
44  *
45  * @author Rod Johnson
46  * @author Juergen Hoeller
47  * @see #setPath
48  */

49 public class BindTag extends HtmlEscapingAwareTag {
50
51     /**
52      * Name of the exposed variable within the scope of this tag: "status".
53      */

54     public static final String JavaDoc STATUS_VARIABLE_NAME = "status";
55
56
57     private String JavaDoc path;
58
59     private boolean ignoreNestedPath = false;
60
61     private BindStatus status;
62
63     private Object JavaDoc previousPageStatus;
64
65     private Object JavaDoc previousRequestStatus;
66
67
68     /**
69      * Set the path that this tag should apply. Can be a bean (e.g. "person")
70      * to get global errors, or a bean property (e.g. "person.name") to get
71      * field errors (also supporting nested fields and "person.na*" mappings).
72      * "person.*" will return all errors for the specified bean, both global
73      * and field errors.
74      * @see org.springframework.validation.Errors#getGlobalErrors
75      * @see org.springframework.validation.Errors#getFieldErrors
76      */

77     public void setPath(String JavaDoc path) {
78         this.path = path;
79     }
80
81     /**
82      * Return the path that this tag applies to.
83      */

84     public String JavaDoc getPath() {
85         return this.path;
86     }
87
88     /**
89      * Set whether to ignore a nested path, if any.
90      * Default is to not ignore.
91      */

92     public void setIgnoreNestedPath(boolean ignoreNestedPath) {
93       this.ignoreNestedPath = ignoreNestedPath;
94     }
95
96     /**
97      * Return whether to ignore a nested path, if any.
98      */

99     public boolean isIgnoreNestedPath() {
100       return this.ignoreNestedPath;
101     }
102
103
104     protected final int doStartTagInternal() throws Exception JavaDoc {
105         String JavaDoc resolvedPath = ExpressionEvaluationUtils.evaluateString("path", getPath(), pageContext);
106
107         if (!isIgnoreNestedPath()) {
108             String JavaDoc nestedPath = (String JavaDoc) pageContext.getAttribute(
109                     NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
110             if (nestedPath != null) {
111                 resolvedPath = nestedPath + resolvedPath;
112             }
113         }
114
115         try {
116             this.status = new BindStatus(getRequestContext(), resolvedPath, isHtmlEscape());
117         }
118         catch (IllegalStateException JavaDoc ex) {
119             throw new JspTagException JavaDoc(ex.getMessage());
120         }
121
122         // Save previous status values, for re-exposure at the end of this tag.
123
this.previousPageStatus = pageContext.getAttribute(STATUS_VARIABLE_NAME, PageContext.PAGE_SCOPE);
124         this.previousRequestStatus = pageContext.getAttribute(STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
125
126         // Expose this tag's status object as PageContext attribute,
127
// making it available for JSP EL.
128
pageContext.removeAttribute(STATUS_VARIABLE_NAME, PageContext.PAGE_SCOPE);
129         pageContext.setAttribute(STATUS_VARIABLE_NAME, this.status, PageContext.REQUEST_SCOPE);
130
131         return EVAL_BODY_INCLUDE;
132     }
133
134     public int doEndTag() {
135         // Reset previous status values.
136
if (this.previousPageStatus != null) {
137             pageContext.setAttribute(STATUS_VARIABLE_NAME, this.previousPageStatus, PageContext.PAGE_SCOPE);
138         }
139         if (this.previousRequestStatus != null) {
140             pageContext.setAttribute(STATUS_VARIABLE_NAME, this.previousRequestStatus, PageContext.REQUEST_SCOPE);
141         }
142         else {
143             pageContext.removeAttribute(STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
144         }
145         return EVAL_PAGE;
146     }
147
148
149     /**
150      * Retrieve the property that this tag is currently bound to,
151      * or <code>null</code> if bound to an object rather than a specific property.
152      * Intended for cooperating nesting tags.
153      * @return the property that this tag is currently bound to,
154      * or <code>null</code> if none
155      */

156     public final String JavaDoc getProperty() {
157         return this.status.getExpression();
158     }
159
160     /**
161      * Retrieve the Errors instance that this tag is currently bound to.
162      * Intended for cooperating nesting tags.
163      * @return the current Errors instance, or <code>null</code> if none
164      */

165     public final Errors getErrors() {
166         return this.status.getErrors();
167     }
168
169     /**
170      * Retrieve the PropertyEditor for the property that this tag is
171      * currently bound to. Intended for cooperating nesting tags.
172      * @return the current PropertyEditor, or <code>null</code> if none
173      */

174     public final PropertyEditor JavaDoc getEditor() {
175         return this.status.getEditor();
176     }
177
178
179     public void doFinally() {
180         super.doFinally();
181         this.status = null;
182         this.previousPageStatus = null;
183         this.previousRequestStatus = null;
184     }
185
186 }
187
Popular Tags