KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > struts > ErrorsTool


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
18 package org.apache.velocity.tools.struts;
19
20 import org.apache.struts.action.ActionErrors;
21
22 /**
23  * <p>View tool to work with the Struts error messages.</p>
24  * <p><pre>
25  * Template example(s):
26  * #if( $errors.exist() )
27  * &lt;div class="errors"&gt;
28  * #foreach( $e in $errors.all )
29  * $e &lt;br&gt;
30  * #end
31  * &lt;/div&gt;
32  * #end
33  *
34  * Toolbox configuration:
35  * &lt;tool&gt;
36  * &lt;key&gt;errors&lt;/key&gt;
37  * &lt;scope&gt;request&lt;/scope&gt;
38  * &lt;class&gt;org.apache.velocity.tools.struts.ErrorsTool&lt;/class&gt;
39  * &lt;/tool&gt;
40  * </pre></p>
41  *
42  * <p>This tool should only be used in the request scope.</p>
43  * <p>Since VelocityTools 1.1, ErrorsTool extends ActionMessagesTool.</p>
44  *
45  * @author <a HREF="mailto:sidler@teamup.com">Gabe Sidler</a>
46  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
47  * @since VelocityTools 1.0
48  * @version $Id: ErrorsTool.java,v 1.12.2.1 2004/03/12 23:36:19 nbubna Exp $
49  */

50 public class ErrorsTool extends ActionMessagesTool
51 {
52     
53     /**
54      * Initializes this tool.
55      *
56      * @param obj the current ViewContext
57      * @throws IllegalArgumentException if the param is not a ViewContext
58      */

59     public void init(Object JavaDoc obj)
60     {
61         //setup superclass instance members
62
super.init(obj);
63
64         this.actionMsgs = StrutsUtils.getErrors(this.request);
65     }
66
67
68     /**
69      * <p>Renders the queued error messages as a list. This method expects
70      * the message keys <code>errors.header</code> and <code>errors.footer</code>
71      * in the message resources. The value of the former is rendered before
72      * the list of error messages and the value of the latter is rendered
73      * after the error messages.</p>
74      *
75      * @return The formatted error messages. If no error messages are queued,
76      * an empty string is returned.
77      */

78     public String JavaDoc getMsgs()
79     {
80         return getMsgs(null, null);
81     }
82
83
84     /**
85      * <p>Renders the queued error messages of a particual category as a list.
86      * This method expects the message keys <code>errors.header</code> and
87      * <code>errors.footer</code> in the message resources. The value of the
88      * former is rendered before the list of error messages and the value of
89      * the latter is rendered after the error messages.</p>
90      *
91      * @param property the category of errors to render
92      *
93      * @return The formatted error messages. If no error messages are queued,
94      * an empty string is returned.
95      */

96     public String JavaDoc getMsgs(String JavaDoc property)
97     {
98         return getMsgs(property, null);
99     }
100
101
102     /**
103      * <p>Renders the queued error messages of a particual category as a list.
104      * This method expects the message keys <code>errors.header</code> and
105      * <code>errors.footer</code> in the message resources. The value of the
106      * former is rendered before the list of error messages and the value of
107      * the latter is rendered after the error messages.</p>
108      *
109      * @param property the category of errors to render
110      * @param bundle the message resource bundle to use
111      *
112      * @return The formatted error messages. If no error messages are queued,
113      * an empty string is returned.
114      * @since VelocityTools 1.1
115      */

116     public String JavaDoc getMsgs(String JavaDoc property, String JavaDoc bundle)
117     {
118         return StrutsUtils.errorMarkup(property, bundle, request,
119                                        request.getSession(false), application);
120     }
121
122
123     /**
124      * Overrides {@link ActionMessagesTool#getGlobalName()}
125      * to return the "global" key for action errors.
126      *
127      * @see org.apache.struts.action.ActionErrors.GLOBAL_ERROR
128      * @deprecated This will be removed after VelocityTools 1.1.
129      */

130     public String JavaDoc getGlobalName()
131     {
132         return ActionErrors.GLOBAL_ERROR;
133     }
134
135 }
136
Popular Tags