KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > form > FormTag


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.form;
8
9
10 import java.util.Iterator JavaDoc;
11
12 import javax.servlet.jsp.JspException JavaDoc;
13
14 import org.apache.log4j.Logger;
15
16 import com.inversoft.beans.BeanException;
17 import com.inversoft.util.StringTools;
18 import com.inversoft.verge.mvc.MVCException;
19 import com.inversoft.verge.mvc.MVCRequest;
20 import com.inversoft.verge.mvc.controller.form.FormMVCMetaData;
21 import com.inversoft.verge.mvc.controller.form.FormURLTools;
22 import com.inversoft.verge.mvc.controller.form.config.FormConfig;
23 import com.inversoft.verge.mvc.controller.form.config.FormConfigStruct;
24 import com.inversoft.verge.mvc.model.ModelResolution;
25 import com.inversoft.verge.mvc.model.form.FormMetaData;
26 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
27 import com.inversoft.verge.mvc.view.ViewConstants;
28 import com.inversoft.verge.mvc.view.jsp.JspTools;
29 import com.inversoft.verge.mvc.view.jsp.model.ModelResolutionRegistry;
30 import com.inversoft.verge.util.WebBean;
31
32
33 /**
34  * This class is the form based MVC form tag.
35  *
36  * @author Brian Pontarelli
37  */

38 public class FormTag extends com.inversoft.verge.mvc.view.jsp.html.FormTag {
39
40     /**
41      * This classes logger
42      */

43     private static final Logger logger = Logger.getLogger(FormTag.class);
44
45
46     private String JavaDoc form;
47     protected String JavaDoc localForm;
48     private String JavaDoc var;
49     protected FormMVCMetaData metaData;
50     protected FormConfig config;
51
52
53     /**
54      * Retrieves the tag's form attribute
55      *
56      * @return The tag's form attribute
57      */

58     public String JavaDoc getForm() {
59         return form;
60     }
61
62     /**
63      * Populates the tag's form attribute
64      *
65      * @param form The tag's form attribute
66      */

67     public void setForm(String JavaDoc form) {
68         this.form = form;
69     }
70
71     /**
72      * Retrieves the tags var attribute
73      *
74      * @return The tags var attribute
75      */

76     public String JavaDoc getVar() {
77         return var;
78     }
79
80     /**
81      * Populates the tags var attribute
82      *
83      * @param var The tags var attribute
84      */

85     public void setVar(String JavaDoc var) {
86         this.var = var;
87     }
88
89     /**
90      * Returns the meta data of this form
91      *
92      * @return The meta data of this form
93      */

94     public FormMVCMetaData getMetaData() {
95         return metaData;
96     }
97
98     /**
99      * Returns the config of this form
100      *
101      * @return The config of this form
102      */

103     public FormConfig getConfig() {
104         return config;
105     }
106
107     /**
108      * Expands the variables if needed
109      */

110     protected void initialize() throws JspException JavaDoc {
111         super.initialize();
112
113         localForm = form;
114
115         if (!JspTools.JSP_20) {
116             localForm = (String JavaDoc) JspTools.expand("form", form, String JavaDoc.class,
117                 this, pageContext);
118         }
119     }
120
121     /**
122      * Outputs the opening form tag with the name, method and attributes given
123      * supplied on the JSP page.
124      *
125      * @return Always returns EVAL_BODY_INCLUDE
126      * @throws JspException If there was a problem outputting the HTML
127      */

128     public int doStartTag() throws JspException JavaDoc {
129         initialize();
130         setupForm();
131
132         return super.doStartTag();
133     }
134
135     /**
136      * Sets up the form including all the form beans and meta data needed to
137      * render the form to the client.
138      *
139      * @throws JspException If anything during setup went wrong
140      */

141     void setupForm() throws JspException JavaDoc {
142         metaData = new FormMVCMetaData(localForm, null);
143
144         try {
145             config = (FormConfig) metaData.findFormConfig(pageContext.getRequest());
146         } catch (MVCException mvce) {
147             logger.error(mvce);
148             throw new JspException JavaDoc("Invalid form name: " + localForm, mvce);
149         }
150
151         // Store the FormConfig in the MVCRequest so that the model tags can use
152
// it
153
MVCRequest mvcRequest = JspTools.getMVCRequest(pageContext);
154         FormConfigStruct struct = new FormConfigStruct(config, null, metaData);
155         mvcRequest.setConfiguration(struct);
156
157         // The contract that only a form with a single form bean and the var
158
// attribute specified is verified in the TEI class. Here we just grab
159
// all the form beans and stuff them into the page context and the MVC
160
// stores
161
boolean hasVar = !StringTools.isEmpty(var);
162         Iterator JavaDoc iter = config.getFormBeans().iterator();
163         WebBean webBean;
164         Object JavaDoc bean;
165         String JavaDoc id;
166         FormMetaData md;
167         ModelResolution modelResolution;
168
169         while (iter.hasNext()) {
170             webBean = (WebBean) iter.next();
171
172             try {
173                 bean = webBean.getInstance(pageContext);
174             } catch (BeanException be) {
175                 logger.error(be);
176                 throw new JspException JavaDoc(be);
177             }
178
179             // Uses the var name if specified. Since var is only used with a
180
// single form bean form, this is okay
181
if (hasVar) {
182                 id = var;
183             } else {
184                 id = webBean.getID();
185             }
186             pageContext.setAttribute(id, bean);
187
188             // Store the ModelResolution for the model tags
189
md = new FormMetaData(webBean.getID(), null);
190             modelResolution = new ModelResolution(bean, md);
191             ModelResolutionRegistry.store(id, modelResolution, pageContext);
192         }
193
194     }
195
196     /**
197      * Renders the HTML form start tag and appends it to the StringBuffer given.
198      * This actually delegates most of its work to the super implementation of
199      * this method and only overrides the computation of the action url and
200      * possibly the form name..
201      *
202      * @throws JspException If anything went wrong rendering the form
203      */

204     protected void createForm(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name, String JavaDoc url,
205         String JavaDoc method)
206     throws JspException JavaDoc {
207         // If there is no name of the form, use the configuration name
208
if (super.getName() == null) {
209             name = localForm;
210         }
211
212         String JavaDoc localURL = FormURLTools.generateURL(localForm, null);
213         super.createForm(buf, id, name, localURL, method);
214
215         HtmlViewToolkit.appendHiddenTag(buf, ViewConstants.MVC_HIDDEN_1, null);
216         HtmlViewToolkit.appendHiddenTag(buf, ViewConstants.MVC_HIDDEN_2, null);
217     }
218 }
Popular Tags