KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > strutsutil > FormBeanUtil


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7  http://www.apache.org/licenses/LICENSE-2.0
8
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15
16 package com.jdon.strutsutil;
17
18 import org.apache.struts.action.ActionMapping;
19 import org.apache.struts.action.ActionForm;
20 import org.apache.struts.action.ActionErrors;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22
23 import javax.servlet.http.HttpSession JavaDoc;
24 import com.jdon.controller.events.EventModel;
25 import org.apache.commons.beanutils.PropertyUtils;
26 import com.jdon.util.Debug;
27 import com.jdon.controller.events.EventSupport;
28 import java.security.Principal JavaDoc;
29 import com.jdon.controller.model.ModelIF;
30 import com.jdon.model.ModelForm;
31 import org.apache.struts.config.ModuleConfig;
32 import org.apache.struts.config.FormBeanConfig;
33 import org.apache.struts.util.ModuleUtils;
34 import org.apache.struts.action.ActionMessages;
35 import org.apache.struts.action.ActionMessage;
36
37 /**
38  * 工具类,相关ActionForm 或Model之类的工具箱
39  *
40  * <p>Copyright: Jdon.com Copyright (c) 2003</p>
41  * <p>Company: </p>
42  * @author banq
43  * @version 1.0
44  */

45 public final class FormBeanUtil {
46
47     public final static String JavaDoc module = FormBeanUtil.class.getName();
48
49     private final static ModuleUtils moduleUtils = ModuleUtils.getInstance();
50
51     public final static String JavaDoc FORWARD_SUCCESS_NAME = "success";
52
53     public final static String JavaDoc FORWARD_FAILURE_NAME = "failure";
54
55     /**
56      * å°†ActionFormä¿?存在struts_config.xml定义的attribute中
57      * @param form
58      * @param mapping
59      * @param request
60      */

61     public static void saveActionForm(ActionForm form, ActionMapping mapping,
62             HttpServletRequest JavaDoc request) {
63         if ((form != null) && (mapping.getAttribute() != null)) {
64             if ("request".equals(mapping.getScope())) {
65                 request.setAttribute(mapping.getAttribute(), form);
66             } else {
67                 HttpSession JavaDoc session = request.getSession();
68                 session.setAttribute(mapping.getAttribute(), form);
69                 request.setAttribute(mapping.getAttribute(), form);
70             }
71         }
72     }
73
74     /**
75      * å°†ä¿?存在struts_config.xml定义的attribute中ActionFormå?–出
76      * @param form
77      * @param mapping
78      * @param request
79      */

80     public static ActionForm loadActionForm(ActionMapping mapping,
81             HttpServletRequest JavaDoc request) {
82         if ("request".equals(mapping.getScope())) {
83             return (ActionForm) request.getAttribute(mapping.getAttribute());
84         } else {
85             HttpSession JavaDoc session = request.getSession();
86             return (ActionForm) session.getAttribute(mapping.getAttribute());
87         }
88     }
89
90     /**
91      * lookup ActionForm in
92      * @param request
93      * @return
94      */

95     public static ActionForm lookupActionForm(HttpServletRequest JavaDoc request,
96             String JavaDoc formName) {
97         ActionForm actionForm = null;
98         actionForm = (ActionForm) request.getAttribute(formName);
99         if (actionForm == null) {
100             HttpSession JavaDoc session = request.getSession();
101             actionForm = (ActionForm) session.getAttribute(formName);
102         }
103         return actionForm;
104     }
105
106     /**
107      * 删除ä¿?存在attribute中的ActionForm实例
108      * @param mapping
109      * @param request
110      */

111     public static void removeActionForm(ActionMapping mapping,
112             HttpServletRequest JavaDoc request) {
113         if (mapping.getAttribute() != null) {
114             if ("request".equals(mapping.getScope()))
115                 request.removeAttribute(mapping.getAttribute());
116             else {
117                 HttpSession JavaDoc session = request.getSession();
118                 session.removeAttribute(mapping.getAttribute());
119                 request.removeAttribute(mapping.getAttribute());
120             }
121         }
122     }
123
124     public static String JavaDoc getFormName(ActionMapping mapping) throws Exception JavaDoc {
125         String JavaDoc formName = "NoFormName Error!";
126         if (mapping.getName() != null)
127             formName = mapping.getName();
128         else if ((mapping.getAttribute() != null))
129             formName = mapping.getAttribute();
130         else
131             throw new Exception JavaDoc("not found the actionForm name in action configure");
132         return formName;
133     }
134
135     public static ModelForm getModelForm(ActionMapping actionMapping,
136             ActionForm actionForm, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
137
138         if (actionForm == null) {
139             String JavaDoc msg = " not found the actionForm name in action configure";
140             Debug.logError(msg);
141             throw new Exception JavaDoc(msg);
142             //modelForm = createModelFormNow(actionMapping, actionForm, request);
143
}
144         
145         ModelForm modelForm = null;
146         try {
147             modelForm = (ModelForm) actionForm;
148         } catch (ClassCastException JavaDoc e) {
149             String JavaDoc msg = "your class:" + actionForm.getClass().getName()
150                     + " isn't the subclass of com.jdon.model.ModelForm";
151             Debug.logVerbose(msg, module);
152             throw new Exception JavaDoc(msg);
153         }
154         
155         return modelForm;
156     }
157
158     /**
159      * æ ¹æ?®struts-config.xmlé…?置立å?³åˆ›å»ºActionForm
160      * @param actionMapping ActionMapping
161      * @param actionForm ActionForm
162      * @param request HttpServletRequest
163      * @param moduleConfig ModuleConfig
164      * @return ModelForm
165      * @throws Exception
166      */

167     private static ModelForm createModelFormNow(ActionMapping actionMapping,
168             ActionForm actionForm, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
169
170         Debug.logVerbose(
171                 "[JdonFramework] not found a existed ModelForm, create it now",
172                 module);
173         ModuleConfig moduleConfig = moduleUtils.getModuleConfig(request,
174                 request.getSession().getServletContext());
175         ModelForm form = null;
176         String JavaDoc formName = null;
177         String JavaDoc formClass = null;
178         try {
179             formName = getFormName(actionMapping);
180             FormBeanConfig formConfig = moduleConfig.findFormBeanConfig(formName);
181             if (formConfig == null) {
182                 throw new Exception JavaDoc(" not found config for " + formName);
183             }
184             formClass = formConfig.getType();
185
186             ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
187             form = (ModelForm) classLoader.loadClass(formClass).newInstance();
188
189             String JavaDoc action = request.getParameter("action");
190             if (action == null)
191                 action = request.getParameter("method");
192             form.setAction(action);
193
194             request.setAttribute(formName, form);
195         } catch (Exception JavaDoc ex) {
196             Debug.logError("[JdonFramework] formName:" + formName
197                     + "formClass create error :" + formClass + ex, module);
198         }
199         return form;
200     }
201
202     public static boolean validateAction(String JavaDoc actionName,
203             ActionMapping mapping) {
204         boolean res = true;
205         int result = actionTransfer(actionName); //如果没有使用规定å??称
206
if (result == 0)
207             res = false;
208
209         if (mapping.findForward(actionName) == null) //如果é…?置文件没有该å??称
210
res = false;
211
212         return res;
213
214     }
215
216     public static String JavaDoc getName(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
217         Principal JavaDoc principal = request.getUserPrincipal();
218         if (principal == null) {
219             Debug.logError("[JdonFramework] No Principal", module);
220             throw new Exception JavaDoc(" No Principal");
221         }
222         return principal.getName();
223     }
224
225     public static ActionErrors notNull(Object JavaDoc object, String JavaDoc errorsInfo) {
226         ActionErrors errors = new ActionErrors();
227         String JavaDoc Id = (String JavaDoc) object;
228         if (object == null) {
229             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
230                     errorsInfo));
231         }
232         return errors;
233     }
234
235     /**
236      * create a EventModel from a existed ModelForm.
237      * it is only for create/edit/delete of ModelSaveAction
238      */

239     public static EventModel createEvent(ModelForm form, ModelIF model)
240             throws Exception JavaDoc {
241         EventModel em = new EventModel();
242         try {
243             PropertyUtils.copyProperties(model, form);
244             em.setModelIF(model);
245             String JavaDoc action = form.getAction();
246             em.setActionName(action);
247             em.setActionType(FormBeanUtil.actionTransfer(action));
248         } catch (Exception JavaDoc ex) {
249             Debug.logError("[JdonFramework]create Event error:" + ex, module);
250             throw new Exception JavaDoc(ex);
251         }
252         return em;
253     }
254
255     public static int actionTransfer(String JavaDoc actionName) {
256         if (actionName.equalsIgnoreCase(ModelForm.CREATE_STR))
257             return EventSupport.CREATE;
258         else if (actionName.equalsIgnoreCase(ModelForm.EDIT_STR))
259             return EventSupport.EDIT;
260         else if (actionName.equalsIgnoreCase(ModelForm.UPDATE_STR))
261             return EventSupport.EDIT;
262         else if (actionName.equalsIgnoreCase(ModelForm.DELETE_STR))
263             return EventSupport.DELETE;
264         else
265             return 0;
266     }
267
268 }
269
Popular Tags