001/*
002 * Copyright 2002-2018 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.lang.Nullable;
024import org.springframework.validation.Errors;
025
026/**
027 * This {@code <hasBindErrors>} tag provides an {@link Errors} instance in case of
028 * bind errors. The HTML escaping flag participates in a page-wide or
029 * application-wide setting (i.e. by HtmlEscapeTag or a "defaultHtmlEscape"
030 * context-param in web.xml).
031 *
032 * <table>
033 * <caption>Attribute Summary</caption>
034 * <thead>
035 * <tr>
036 * <th>Attribute</th>
037 * <th>Required?</th>
038 * <th>Runtime Expression?</th>
039 * <th>Description</th>
040 * </tr>
041 * </thead>
042 * <tbody>
043 * <tr>
044 * <td>htmlEscape</td>
045 * <td>false</td>
046 * <td>true</td>
047 * <td>Set HTML escaping for this tag, as boolean value.
048 * Overrides the default HTML escaping setting for the current page.</td>
049 * </tr>
050 * <tr>
051 * <td>name</td>
052 * <td>true</td>
053 * <td>true</td>
054 * <td>The name of the bean in the request that needs to be inspected for errors.
055 * If errors are available for this bean, they will be bound under the
056 * 'errors' key.</td>
057 * </tr>
058 * </tbody>
059 * </table>
060 *
061 * @author Rod Johnson
062 * @author Juergen Hoeller
063 * @see BindTag
064 * @see org.springframework.validation.Errors
065 */
066@SuppressWarnings("serial")
067public class BindErrorsTag extends HtmlEscapingAwareTag {
068
069        /**
070         * Page context attribute containing {@link Errors}.
071         */
072        public static final String ERRORS_VARIABLE_NAME = "errors";
073
074
075        private String name = "";
076
077        @Nullable
078        private Errors errors;
079
080
081        /**
082         * Set the name of the bean that this tag should check.
083         */
084        public void setName(String name) {
085                this.name = name;
086        }
087
088        /**
089         * Return the name of the bean that this tag checks.
090         */
091        public String getName() {
092                return this.name;
093        }
094
095
096        @Override
097        protected final int doStartTagInternal() throws ServletException, JspException {
098                this.errors = getRequestContext().getErrors(this.name, isHtmlEscape());
099                if (this.errors != null && this.errors.hasErrors()) {
100                        this.pageContext.setAttribute(ERRORS_VARIABLE_NAME, this.errors, PageContext.REQUEST_SCOPE);
101                        return EVAL_BODY_INCLUDE;
102                }
103                else {
104                        return SKIP_BODY;
105                }
106        }
107
108        @Override
109        public int doEndTag() {
110                this.pageContext.removeAttribute(ERRORS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
111                return EVAL_PAGE;
112        }
113
114        /**
115         * Retrieve the Errors instance that this tag is currently bound to.
116         * <p>Intended for cooperating nesting tags.
117         */
118        @Nullable
119        public final Errors getErrors() {
120                return this.errors;
121        }
122
123
124        @Override
125        public void doFinally() {
126                super.doFinally();
127                this.errors = null;
128        }
129
130}