001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet.tags;
018:
019: import java.beans.PropertyEditor;
020:
021: import javax.servlet.jsp.JspTagException;
022: import javax.servlet.jsp.PageContext;
023:
024: import org.springframework.validation.Errors;
025: import org.springframework.web.servlet.support.BindStatus;
026: import org.springframework.web.util.ExpressionEvaluationUtils;
027:
028: /**
029: * Bind tag, supporting evaluation of binding errors for a certain
030: * bean or bean property. Exposes a "status" variable of type
031: * {@link org.springframework.web.servlet.support.BindStatus},
032: * to both Java expressions and JSP EL expressions.
033: *
034: * <p>Can be used to bind to any bean or bean property in the model.
035: * The specified path determines whether the tag exposes the status of the
036: * bean itself (showing object-level errors), a specific bean property
037: * (showing field errors), or a matching set of bean properties
038: * (showing all corresponding field errors).
039: *
040: * <p>The {@link org.springframework.validation.Errors} object that has
041: * been bound using this tag is exposed to collaborating tags, as well
042: * as the bean property that this errors object applies to. Nested tags
043: * such as the {@link TransformTag} can access those exposed properties.
044: *
045: * @author Rod Johnson
046: * @author Juergen Hoeller
047: * @see #setPath
048: */
049: public class BindTag extends HtmlEscapingAwareTag {
050:
051: /**
052: * Name of the exposed variable within the scope of this tag: "status".
053: */
054: public static final String STATUS_VARIABLE_NAME = "status";
055:
056: private String path;
057:
058: private boolean ignoreNestedPath = false;
059:
060: private BindStatus status;
061:
062: private Object previousPageStatus;
063:
064: private Object previousRequestStatus;
065:
066: /**
067: * Set the path that this tag should apply. Can be a bean (e.g. "person")
068: * to get global errors, or a bean property (e.g. "person.name") to get
069: * field errors (also supporting nested fields and "person.na*" mappings).
070: * "person.*" will return all errors for the specified bean, both global
071: * and field errors.
072: * @see org.springframework.validation.Errors#getGlobalErrors
073: * @see org.springframework.validation.Errors#getFieldErrors
074: */
075: public void setPath(String path) {
076: this .path = path;
077: }
078:
079: /**
080: * Return the path that this tag applies to.
081: */
082: public String getPath() {
083: return this .path;
084: }
085:
086: /**
087: * Set whether to ignore a nested path, if any.
088: * Default is to not ignore.
089: */
090: public void setIgnoreNestedPath(boolean ignoreNestedPath) {
091: this .ignoreNestedPath = ignoreNestedPath;
092: }
093:
094: /**
095: * Return whether to ignore a nested path, if any.
096: */
097: public boolean isIgnoreNestedPath() {
098: return this .ignoreNestedPath;
099: }
100:
101: protected final int doStartTagInternal() throws Exception {
102: String resolvedPath = ExpressionEvaluationUtils.evaluateString(
103: "path", getPath(), pageContext);
104:
105: if (!isIgnoreNestedPath()) {
106: String nestedPath = (String) pageContext.getAttribute(
107: NestedPathTag.NESTED_PATH_VARIABLE_NAME,
108: PageContext.REQUEST_SCOPE);
109: if (nestedPath != null) {
110: resolvedPath = nestedPath + resolvedPath;
111: }
112: }
113:
114: try {
115: this .status = new BindStatus(getRequestContext(),
116: resolvedPath, isHtmlEscape());
117: } catch (IllegalStateException ex) {
118: throw new JspTagException(ex.getMessage());
119: }
120:
121: // Save previous status values, for re-exposure at the end of this tag.
122: this .previousPageStatus = pageContext.getAttribute(
123: STATUS_VARIABLE_NAME, PageContext.PAGE_SCOPE);
124: this .previousRequestStatus = pageContext.getAttribute(
125: STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
126:
127: // Expose this tag's status object as PageContext attribute,
128: // making it available for JSP EL.
129: pageContext.removeAttribute(STATUS_VARIABLE_NAME,
130: PageContext.PAGE_SCOPE);
131: pageContext.setAttribute(STATUS_VARIABLE_NAME, this .status,
132: PageContext.REQUEST_SCOPE);
133:
134: return EVAL_BODY_INCLUDE;
135: }
136:
137: public int doEndTag() {
138: // Reset previous status values.
139: if (this .previousPageStatus != null) {
140: pageContext.setAttribute(STATUS_VARIABLE_NAME,
141: this .previousPageStatus, PageContext.PAGE_SCOPE);
142: }
143: if (this .previousRequestStatus != null) {
144: pageContext.setAttribute(STATUS_VARIABLE_NAME,
145: this .previousRequestStatus,
146: PageContext.REQUEST_SCOPE);
147: } else {
148: pageContext.removeAttribute(STATUS_VARIABLE_NAME,
149: PageContext.REQUEST_SCOPE);
150: }
151: return EVAL_PAGE;
152: }
153:
154: /**
155: * Retrieve the property that this tag is currently bound to,
156: * or <code>null</code> if bound to an object rather than a specific property.
157: * Intended for cooperating nesting tags.
158: * @return the property that this tag is currently bound to,
159: * or <code>null</code> if none
160: */
161: public final String getProperty() {
162: return this .status.getExpression();
163: }
164:
165: /**
166: * Retrieve the Errors instance that this tag is currently bound to.
167: * Intended for cooperating nesting tags.
168: * @return the current Errors instance, or <code>null</code> if none
169: */
170: public final Errors getErrors() {
171: return this .status.getErrors();
172: }
173:
174: /**
175: * Retrieve the PropertyEditor for the property that this tag is
176: * currently bound to. Intended for cooperating nesting tags.
177: * @return the current PropertyEditor, or <code>null</code> if none
178: */
179: public final PropertyEditor getEditor() {
180: return this .status.getEditor();
181: }
182:
183: public void doFinally() {
184: super.doFinally();
185: this.status = null;
186: this.previousPageStatus = null;
187: this.previousRequestStatus = null;
188: }
189:
190: }
|