KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > taglib > FormActionTag


1 package com.jcorporate.expresso.services.taglib;
2
3 /* ====================================================================
4  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
5  *
6  * Copyright (c) 1995-2003 Jcorporate Ltd. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by Jcorporate Ltd.
23  * (http://www.jcorporate.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. "Jcorporate" and product names such as "Expresso" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written permission,
30  * please contact info@jcorporate.com.
31  *
32  * 5. Products derived from this software may not be called "Expresso",
33  * or other Jcorporate product names; nor may "Expresso" or other
34  * Jcorporate product names appear in their name, without prior
35  * written permission of Jcorporate Ltd.
36  *
37  * 6. No product derived from this software may compete in the same
38  * market space, i.e. framework, without prior written permission
39  * of Jcorporate Ltd. For written permission, please contact
40  * partners@jcorporate.com.
41  *
42  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
43  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
46  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
48  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
49  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
51  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
52  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  * ====================================================================
55  *
56  * This software consists of voluntary contributions made by many
57  * individuals on behalf of the Jcorporate Ltd. Contributions back
58  * to the project(s) are encouraged when you make modifications.
59  * Please send them to support@jcorporate.com. For more information
60  * on Jcorporate Ltd. and its products, please see
61  * <http://www.jcorporate.com/>.
62  *
63  * Portions of this software are based upon other open source
64  * products and are subject to their respective licenses.
65  */

66
67 import org.apache.log4j.Logger;
68
69 import javax.servlet.http.HttpServletRequest JavaDoc;
70 import javax.servlet.http.HttpServletResponse JavaDoc;
71 import javax.servlet.jsp.JspException JavaDoc;
72 import javax.servlet.jsp.JspWriter JavaDoc;
73 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
74 import java.io.IOException JavaDoc;
75
76
77 /**
78  * Use FormActionTag for the &quot;action=&quot; part of the &lt;form&gt; tag.
79  * The tag takes a transition and automatically maps it to the appropriate
80  * URL that should be used for the form.
81  * <p>This tag's parameters mirror that of JSTL's &lt;c:out/&gt; tag with the
82  * only exception being that the 'escapeXML' attribute is not available.</p>
83  * <p>The tag parameters should evaluate to a string. The way to do this with
84  * a Transition is to evaluate to the transition and call getMapping()</p>
85  * <p>So an example would be:
86  * <blockquote><pre>
87  * <expresso-el:action value="${controllerResponse.namedTransitions['submit'].mapping}"/>
88  * </pre></blockquote>
89  * </p>
90  *
91  * @author Michael Rimov
92  * @version $Revision: 1.4 $ on $Date: 2004/11/17 20:48:21 $
93  */

94 public class FormActionTag extends TagSupport JavaDoc {
95
96     private static final Logger log = Logger.getLogger(FormActionTag.class);
97
98     /**
99      * JSTL Expression for the 'value' attribute
100      */

101     private String JavaDoc value;
102
103     /**
104      * JSTL Expression for the 'default' attribute
105      */

106     private String JavaDoc defaultValue;
107
108     /**
109      * Default constructor
110      */

111     public FormActionTag() {
112     }
113
114     /**
115      * Retrieve the value for the tag
116      *
117      * @return java.lang.String
118      */

119     public String JavaDoc getValue() {
120         return value;
121     }
122
123     /**
124      * Sets the value expression for the tag.
125      *
126      * @param value the new value JSTL expression
127      */

128     public void setValue(String JavaDoc value) {
129         this.value = value;
130     }
131
132     /**
133      * Performs the actual guts of the form action tag. Retrieves the
134      * Transition class and evaluates the mapping it should send to.
135      *
136      * @return EVAL_PAGE
137      */

138     public int doEndTag() throws javax.servlet.jsp.JspException JavaDoc {
139         if (log.isDebugEnabled()) {
140             log.debug("Evaluating Action Tag: value=" + this.getValue()
141                     + " default=" + this.getDefault());
142         }
143
144         ELTagSupport support = ELTagSupport.getInstance();
145         Exception JavaDoc ex = null;
146         boolean tryDefault = false;
147         String JavaDoc result = "";
148         try {
149             result = (String JavaDoc) support.evaluate("value", this.getValue(),
150                     String JavaDoc.class,
151                     this, this.pageContext);
152
153             if (result == null || result.length() == 0) {
154                 tryDefault = true;
155             }
156         } catch (ClassCastException JavaDoc ex1) {
157             log.error("ClassCastException evaluating parameter 'value'", ex1);
158             throw new JspException JavaDoc("Received wrong type back for parameter 'value'. Expected java.lang.String");
159         } catch (JspException JavaDoc ex1) {
160             log.info("Failed to resolve value: " + this.getValue(), ex1);
161             ex = ex1;
162             tryDefault = true;
163         }
164
165         String JavaDoc defaultEval = this.getDefault();
166         if (tryDefault && defaultEval != null && defaultEval.length() > 0) {
167             result = (String JavaDoc) support.evaluate("default", getDefault(),
168                     String JavaDoc.class,
169                     this, this.pageContext);
170
171             if (result == null || result.length() == 0) {
172                 log.info("Failed to resolve default: " + this.getDefault());
173                 throw new JspException JavaDoc("Unable to evaluate value or default. 'default'=" + this.getDefault());
174             }
175         } else {
176             if (tryDefault) {
177                 throw new JspException JavaDoc("Unable to evaluate 'value' = " + this.getValue()
178                         + " and no default value supplied");
179             }
180         }
181
182         String JavaDoc writeValue = ((HttpServletRequest JavaDoc) this.pageContext.getRequest()).getContextPath() + result;
183         writeValue = ((HttpServletResponse JavaDoc) this.pageContext.getResponse()).encodeURL(writeValue);
184         JspWriter JavaDoc out = pageContext.getOut();
185         try {
186             out.write(writeValue);
187         } catch (IOException JavaDoc ex2) {
188             log.warn("Error writing : " + writeValue, ex2);
189             throw new JspException JavaDoc("I/O error");
190         }
191
192         return TagSupport.EVAL_PAGE;
193     }
194
195     /**
196      * Retreive the default value for the tag
197      *
198      * @return java.lang.String.
199      */

200     public String JavaDoc getDefault() {
201         return defaultValue;
202     }
203
204     /**
205      * Set the default value for the string
206      *
207      * @param newValue the new default value JSTL expression
208      */

209     public void setDefault(String JavaDoc newValue) {
210         defaultValue = newValue;
211     }
212
213
214 }
Popular Tags