01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.ServletException;
20: import javax.servlet.jsp.JspException;
21: import javax.servlet.jsp.PageContext;
22:
23: import org.springframework.validation.Errors;
24: import org.springframework.web.util.ExpressionEvaluationUtils;
25:
26: /**
27: * Evaluates content if there are bind errors for a certain bean.
28: * Exports an "errors" variable of type Errors for the given bean.
29: *
30: * @author Rod Johnson
31: * @author Juergen Hoeller
32: * @see BindTag
33: * @see org.springframework.validation.Errors
34: */
35: public class BindErrorsTag extends HtmlEscapingAwareTag {
36:
37: public static final String ERRORS_VARIABLE_NAME = "errors";
38:
39: private String name;
40:
41: private Errors errors;
42:
43: /**
44: * Set the name of the bean that this tag should check.
45: */
46: public void setName(String name) {
47: this .name = name;
48: }
49:
50: /**
51: * Return the name of the bean that this tag checks.
52: */
53: public String getName() {
54: return name;
55: }
56:
57: protected final int doStartTagInternal() throws ServletException,
58: JspException {
59: String resolvedName = ExpressionEvaluationUtils.evaluateString(
60: "name", this .name, pageContext);
61: this .errors = getRequestContext().getErrors(resolvedName,
62: isHtmlEscape());
63: if (this .errors != null && this .errors.hasErrors()) {
64: this .pageContext.setAttribute(ERRORS_VARIABLE_NAME,
65: this .errors, PageContext.REQUEST_SCOPE);
66: return EVAL_BODY_INCLUDE;
67: } else {
68: return SKIP_BODY;
69: }
70: }
71:
72: public int doEndTag() {
73: this .pageContext.removeAttribute(ERRORS_VARIABLE_NAME,
74: PageContext.REQUEST_SCOPE);
75: return EVAL_PAGE;
76: }
77:
78: /**
79: * Retrieve the Errors instance that this tag is currently bound to.
80: * Intended for cooperating nesting tags.
81: */
82: public final Errors getErrors() {
83: return errors;
84: }
85:
86: public void doFinally() {
87: super.doFinally();
88: this.errors = null;
89: }
90:
91: }
|