001/*
002 * Copyright 2002-2013 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 *      https://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
017package org.springframework.web.servlet.tags;
018
019import javax.servlet.ServletException;
020import javax.servlet.jsp.JspException;
021import javax.servlet.jsp.PageContext;
022
023import org.springframework.validation.Errors;
024
025/**
026 * JSP tag that evaluates content if there are binding errors
027 * for a certain bean. Exports an "errors" variable of type
028 * {@link org.springframework.validation.Errors} for the given bean.
029 *
030 * @author Rod Johnson
031 * @author Juergen Hoeller
032 * @see BindTag
033 * @see org.springframework.validation.Errors
034 */
035@SuppressWarnings("serial")
036public class BindErrorsTag extends HtmlEscapingAwareTag {
037
038        public static final String ERRORS_VARIABLE_NAME = "errors";
039
040
041        private String name;
042
043        private Errors errors;
044
045
046        /**
047         * Set the name of the bean that this tag should check.
048         */
049        public void setName(String name) {
050                this.name = name;
051        }
052
053        /**
054         * Return the name of the bean that this tag checks.
055         */
056        public String getName() {
057                return this.name;
058        }
059
060
061        @Override
062        protected final int doStartTagInternal() throws ServletException, JspException {
063                this.errors = getRequestContext().getErrors(this.name, isHtmlEscape());
064                if (this.errors != null && this.errors.hasErrors()) {
065                        this.pageContext.setAttribute(ERRORS_VARIABLE_NAME, this.errors, PageContext.REQUEST_SCOPE);
066                        return EVAL_BODY_INCLUDE;
067                }
068                else {
069                        return SKIP_BODY;
070                }
071        }
072
073        @Override
074        public int doEndTag() {
075                this.pageContext.removeAttribute(ERRORS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
076                return EVAL_PAGE;
077        }
078
079        /**
080         * Retrieve the Errors instance that this tag is currently bound to.
081         * <p>Intended for cooperating nesting tags.
082         */
083        public final Errors getErrors() {
084                return this.errors;
085        }
086
087
088        @Override
089        public void doFinally() {
090                super.doFinally();
091                this.errors = null;
092        }
093
094}