KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.jsp.JspException JavaDoc;
12 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
13
14 import com.inversoft.verge.mvc.view.jsp.JspTools;
15 import com.inversoft.verge.util.RequestContext;
16
17
18 /**
19  * This class outputs its body depending on whether or not
20  * there are errors and what the value of the equals property
21  * is. If the equals property is true, then if there are
22  * errors, this outputs its body. If the equals property is
23  * false, and there are errors, this does nothing. And vice
24  * versa for both coniditions both. This tag is useful when
25  * the page needs to contain a message or not, depending on
26  * if there are errors or not.
27  *
28  * @author Brian Pontarelli
29  */

30 public class HasErrorsTag extends TagSupport JavaDoc {
31
32     private Boolean JavaDoc equals;
33     private String JavaDoc property;
34     private String JavaDoc var;
35     private Object JavaDoc value;
36
37
38     public HasErrorsTag() {
39     }
40
41
42     /**
43      * Retrieves the equals attribute used in evaluation
44      *
45      * @return Returns the equals attribute used in evaluation
46      */

47     public Boolean JavaDoc getEquals() {
48         return equals;
49     }
50
51     /**
52      * Populates the equals attribute used in evaluation
53      *
54      * @param equals The value of the equals attribute used in evaluation
55      */

56     public void setEquals(Boolean JavaDoc equals) {
57         this.equals = equals;
58     }
59
60     /**
61      * Retrieves the property attribute used in evaluation
62      *
63      * @return Returns the property attribute used in evaluation
64      */

65     public String JavaDoc getProperty() {
66         return property;
67     }
68
69     /**
70      * Populates the property attribute used in evaluation
71      *
72      * @param property The value of the property attribute used in evaluation
73      */

74     public void setProperty(String JavaDoc property) {
75         this.property = property;
76     }
77
78     /**
79      * Gets the name of the scripting variable to set in addition to evaluating
80      * the body, if the expression is true. This variable is visible to the entire
81      * page
82      *
83      * @return The variable name
84      */

85     public String JavaDoc getVar() {
86         return var;
87     }
88
89     /**
90      * Sets the name of the scripting variable to set in addition to evaluating
91      * the body, if the expression is true. This variable is visible to the entire
92      * page
93      *
94      * @param var The variable name
95      */

96     public void setVar(String JavaDoc var) {
97         this.var = var;
98     }
99
100     /**
101      * Gets the value that the scripting variable is set to, if it is set
102      *
103      * @return The value that the scripting variable is set to
104      */

105     public Object JavaDoc getValue() {
106         return value;
107     }
108
109     /**
110      * Sets the value that the scripting variable is set, if it is set
111      *
112      * @param value The value that the scripting variable is set to
113      */

114     public void setValue(Object JavaDoc value) {
115         this.value = value;
116     }
117
118     /**
119      * This method attempts to get the errors out of the request. If there are
120      * errors, it evalautes the body. If there are NO errors, it does not
121      * evaluate the body.
122      *
123      * @return EVAL_BODY_TAG if there are errors, SKIP_BODY otherwise
124      */

125     public int doStartTag() throws JspException JavaDoc {
126
127         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
128         RequestContext context = new RequestContext(request);
129
130         if (property != null) {
131             property = (String JavaDoc) JspTools.expand("property", property,
132                 String JavaDoc.class, this, pageContext);
133         }
134
135         boolean expr = false;
136         if (property != null) {
137             boolean hasErrors =
138                 ErrorHelper.hasPropertyErrors(property, context, pageContext);
139             expr = (hasErrors == equals.booleanValue());
140         } else {
141             expr = (context.hasErrors() == equals.booleanValue());
142         }
143
144         // Set up the variable if need be
145
if (expr && var != null && value != null) {
146             if (value instanceof String JavaDoc) {
147                 value = JspTools.expand("value", (String JavaDoc) value, Object JavaDoc.class,
148                     this, pageContext);
149             }
150
151             pageContext.setAttribute(var, value);
152         }
153
154         return (expr) ? EVAL_BODY_INCLUDE : SKIP_BODY;
155     }
156 }
157
Popular Tags