KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > error > ErrorsTag


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.error;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.jsp.JspException JavaDoc;
15 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
16
17 import com.inversoft.error.BasicError;
18 import com.inversoft.util.StringTools;
19 import com.inversoft.verge.mvc.view.jsp.JspTools;
20 import com.inversoft.verge.util.RequestContext;
21
22
23 /**
24  * <p>
25  * This class handles form errors by retrieving the errors
26  * from the RequestContext when the servlet sends a forward
27  * request to a JSP page. This tag iterates over all the
28  * errors in the RequestContext and sets up a scripting
29  * variable that contains the error. This scripting variable
30  * can then be used to output the error message
31  * </p>
32  *
33  * <p>
34  * You can also exclude PropertyErrors from the iteration
35  * by setting the excludeProperties attribute to true.
36  * </p>
37  *
38  * <p>
39  * The property attribute allows this tag to be used to
40  * iterate over a single error, which will be a PropertyError
41  * whose property is equal to the value of the property
42  * attribute.
43  * </p>
44  *
45  * @author Brian Pontarelli
46  * @since 2.0
47  * @version 2.0
48  */

49 public class ErrorsTag extends BodyTagSupport JavaDoc {
50
51     private String JavaDoc var;
52     private String JavaDoc property;
53     private Iterator JavaDoc iter;
54     private BasicError error;
55     private boolean excludeProperties;
56
57
58     /**
59      * Empty constructor
60      */

61     public ErrorsTag() {
62     }
63
64
65     /**
66      * Retrieves the attribute that determines the name of the variable this tag
67      * publishes
68      *
69      * @return Returns the attribute that determines the name of the variable
70      * this tag publishes
71      */

72     public String JavaDoc getVar() {
73         return var;
74     }
75
76     /**
77      * Populates the attribute that determines the name of the variable this tag
78      * publishes
79      *
80      * @param var The value of the attribute that determines the name of the
81      * variable this tag publishes
82      */

83     public void setVar(String JavaDoc var) {
84         this.var = var;
85     }
86
87     /**
88      * Retrieves the name of the property whose errors to retrieve
89      *
90      * @return Returns the name of the property whose errors to retrieve
91      */

92     public String JavaDoc getProperty() {
93         return property;
94     }
95
96     /**
97      * Populates the name of the property whose errors to retrieve
98      *
99      * @param property The value of the name of the property whose errors to
100      * retrieve
101      */

102     public void setProperty(String JavaDoc property) {
103         this.property = property;
104     }
105
106     /**
107      * Gets whether or not to exclude property errors from the iteration
108      *
109      * @return True if the property errors should be excluded, false otherwise
110      */

111     public boolean isExcludeProperties() {
112         return excludeProperties;
113     }
114
115     /**
116      * Sets whether or not to exclude property errors from the iteration
117      *
118      * @param excludeProperties True if the property errors should be excluded,
119      * false otherwise
120      */

121     public void setExcludeProperties(boolean excludeProperties) {
122         this.excludeProperties = excludeProperties;
123     }
124
125     /**
126      * This method gets the ActionErrorList out of the request and saves it and
127      * also determines if the tag was requesting a the property errors or
128      * all the errors
129      *
130      * @return SKIP_BODY If the ActionErrorList is empty or null, EVAL_BODY_TAG
131      * otherwise
132      */

133     public int doStartTag() throws JspException JavaDoc {
134         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
135         RequestContext context = new RequestContext(request);
136
137         if (!context.hasErrors()) {
138             return SKIP_BODY;
139         }
140
141         // Try getting it out of the request
142
List JavaDoc list = null;
143
144         if (StringTools.isEmpty(property)) {
145             if (excludeProperties) {
146                 list = context.getBasicErrors();
147             } else {
148                 list = context.getAllErrors();
149             }
150         } else if (!excludeProperties) {
151             // Expand EL expressions
152
property = (String JavaDoc) JspTools.expand("property", property, String JavaDoc.class,
153                 this, pageContext);
154
155             property = ErrorHelper.findPropertyErrors(property, context, pageContext);
156             if (property == null) {
157                 return SKIP_BODY;
158             }
159
160             list = context.getPropertyErrors(property);
161         } else {
162             throw new JspException JavaDoc("You can either specify a property or the " +
163                     "excludeProperties flag but not both");
164         }
165
166         iter = list.iterator();
167         if (!iter.hasNext()) {
168             return SKIP_BODY;
169         }
170
171         // Put the error in the context
172
error = (BasicError) iter.next();
173         pageContext.setAttribute(var, error);
174
175         return EVAL_BODY_INCLUDE;
176     }
177     
178     /**
179      * Tells the tag to either continue processing the errors or to stop
180      *
181      * @return SKIP_BODY if there are no more errors or the page requested a
182      * single property error, otherwise EVAL_BODY_TAG
183      */

184     public int doAfterBody() throws JspException JavaDoc {
185
186         int ret = SKIP_BODY;
187         if (iter.hasNext()) {
188
189             // Put the error in the context
190
error = (BasicError) iter.next();
191             pageContext.setAttribute(var, error);
192
193             ret = EVAL_BODY_AGAIN;
194         }
195
196         return ret;
197     }
198 }
Popular Tags