KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20
21 import org.apache.struts.action.ActionErrors;
22 import org.apache.struts.action.ActionForm;
23 import org.apache.struts.action.ActionForward;
24 import org.apache.struts.action.ActionMapping;
25 import org.apache.struts.action.ActionMessage;
26 import org.apache.struts.action.ActionMessages;
27
28 import com.jdon.controller.model.ModelIF;
29 import com.jdon.model.ModelForm;
30 import com.jdon.util.Debug;
31 import com.jdon.util.UtilValidate;
32
33 /**
34  * this class can be invoked in browser's url
35  * this class will push create/insert view page
36  * or editable view page.
37  *
38  * the push result is according action's value:
39  * 1. null or create ; will push a view page that user can create;
40  * 2. edit; will push a editbale view page that has existed data;
41  *
42  *how to use this class?
43  * 1. this class can be configured in struts-config.xml
44  *
45  * <action name="productForm" type="com.jdon.strutsutil.ModelViewAction" validate="false" scope="request" path="/admin/productAction">
46  * <forward name="create" path="/customer.jsp" />
47  * <forward name="edit" path="/customer.jsp" />
48  * </action>
49  * forward's value is same as action's value
50  * if the action value is not create or edit, the action will directly forward
51  * the action forward page, example: the action vaule is XXXX:
52  * <forward name="XXXX" path="/xxxx.jsp" />
53  *
54  * 2. setup in ApplicationResources.propeties
55  * id.notfound =not found the record (in your language)
56  *
57  * @author banq
58  */

59 public class ModelViewAction extends ModelBaseAction {
60
61     private final static String JavaDoc module = ModelViewAction.class.getName();
62
63
64     /**
65      * accept the form submit, action parameter must be : create or edit;
66      * if not, will directly forward the jsp page mapping for the action value;
67      *
68      *
69      */

70     public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
71             throws Exception JavaDoc {
72         Debug.logVerbose("[JdonFramework]--> enter ModelViewAction process ", module);
73         intContext(this.getServlet().getServletContext());
74
75         ModelForm modelForm = FormBeanUtil.getModelForm(actionMapping, actionForm, request);
76
77         if ((UtilValidate.isEmpty(modelForm.getAction()) ) || modelForm.getAction().equalsIgnoreCase(ModelForm.CREATE_STR)) {
78             Debug.logVerbose("[JdonFramework]--> enter create process ", module);
79             modelForm.setAction(ModelForm.CREATE_STR);
80             createViewPage.doCreate(actionMapping, modelForm, request);
81
82         } else if (modelForm.getAction().equalsIgnoreCase(ModelForm.EDIT_STR)) {
83
84             Debug.logVerbose("[JdonFramework]--> enter " + ModelForm.EDIT_STR + " process ", module);
85             ModelIF model = editeViewPage.getModelForEdit(actionMapping, modelForm, request);
86             if (model == null) //not found the model
87
return errorsForward(modelForm.getAction(), actionMapping, request);
88
89         }else{//other regard as create
90
Debug.logVerbose("[JdonFramework]-->action value not supported, enter create process2 ", module);
91             modelForm.setAction(ModelForm.CREATE_STR);
92             createViewPage.doCreate(actionMapping, modelForm, request);
93         }
94         Debug.logVerbose("[JdonFramework]--> push the jsp that forward name is '" + modelForm.getAction() + "'", module);
95         return actionMapping.findForward(modelForm.getAction());
96     }
97     
98
99     private ActionForward errorsForward(String JavaDoc action, ActionMapping actionMapping, HttpServletRequest JavaDoc request) {
100         ActionMessages errors = new ActionMessages();
101         ActionMessage error = new ActionMessage("id.notfound");
102         errors.add(ActionErrors.GLOBAL_MESSAGE, error);
103         saveErrors(request, errors);
104         if (actionMapping.findForward(FormBeanUtil.FORWARD_FAILURE_NAME) != null)
105             return actionMapping.findForward(FormBeanUtil.FORWARD_FAILURE_NAME);
106         else
107             return actionMapping.findForward(action);
108     }
109
110 }
111
Popular Tags