KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > app1 > AddressAction


1 /*
2  * Copyright 2003 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 package examples.app1;
18
19
20 import java.io.IOException JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Locale JavaDoc;
23 import javax.servlet.RequestDispatcher JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import org.apache.struts.Globals;
29 import org.apache.struts.action.Action;
30 import org.apache.struts.action.ActionError;
31 import org.apache.struts.action.ActionErrors;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.action.ActionServlet;
36 import org.apache.struts.util.MessageResources;
37
38
39 /**
40  * <p>A simple action that handles the display and editing of an
41  * addres record. This action works with both JSP and Velocity templates.
42  * The type of template to be used is defined in the Struts configuration
43  * file.</p>
44  *
45  * <p>The action support an <i>action</i> URL parameter. This URL parameter
46  * controls what this action class does. The following values are supported:</p>
47  * <ul>
48  * <li>list - list address record, this is the default if no action parameter is specified
49  * <li>edit - edit address record
50  * <li>save - save address record
51  * </ul>
52  *
53  *
54  * @author <a HREF="mailto:sidler@teamup.com"/>Gabe Sidler</a>
55  * @version $Id: AddressAction.java,v 1.3 2004/02/20 12:42:47 marino Exp $
56  */

57 public class AddressAction extends Action
58 {
59
60     // --------------------------------------------------------- Public Methods
61

62     /**
63      * Handle server requests.
64      *
65      * @param mapping The ActionMapping used to select this instance
66      * @param actionForm The optional ActionForm bean for this request (if any)
67      * @param request The HTTP request we are processing
68      * @param response The HTTP response we are creating
69      *
70      * @exception IOException if an input/output error occurs
71      * @exception ServletException if a servlet exception occurs
72      */

73     public ActionForward execute(ActionMapping mapping,
74                                  ActionForm form,
75                                  HttpServletRequest JavaDoc request,
76                                  HttpServletResponse JavaDoc response)
77                                  throws IOException JavaDoc, ServletException JavaDoc
78     {
79         String JavaDoc action;
80         HttpSession JavaDoc session;
81
82         ActionErrors errors = new ActionErrors();
83
84         try
85         {
86             session = request.getSession();
87
88             // fetch action from form
89
action = ((AddressForm)form).getAction();
90
91             servlet.log("[DEBUG] AddressAction at perform(): Action ist " + action);
92
93             // Determine what to do
94
if ( action.equals("edit") )
95             {
96                 // forward to edit formular
97
return (mapping.findForward("editAddress"));
98
99             }
100             else if (action.equals("save"))
101             {
102                 // check if an address bean exits already
103
AddressBean bean = (AddressBean)session.getAttribute("address");
104
105                 if (bean == null)
106                 {
107                     bean = new AddressBean();
108                     session.setAttribute("address", bean);
109                 }
110
111                 // update bean with the new values submitted
112
bean.setFirstname( ((AddressForm)form).getFirstname() );
113                 bean.setLastname( ((AddressForm)form).getLastname() );
114                 bean.setStreet( ((AddressForm)form).getStreet() );
115                 bean.setZip( ((AddressForm)form).getZip() );
116                 bean.setCity( ((AddressForm)form).getCity() );
117                 bean.setCountry( ((AddressForm)form).getCountry() );
118                 bean.setLanguages( ((AddressForm)form).getLanguages() );
119
120                 // forward to list
121
return (mapping.findForward("showAddress"));
122
123             }
124             else
125             {
126                 String JavaDoc locale = ((AddressForm)form).getLocale();
127                 if (locale.equals("Deutsch"))
128                     session.setAttribute(Globals.LOCALE_KEY, new Locale JavaDoc("de", ""));
129                 else
130                     session.setAttribute(Globals.LOCALE_KEY, new Locale JavaDoc("en", ""));
131
132                 // forward to edit formular
133
return (mapping.findForward("showAddress"));
134             }
135         }
136         catch (Exception JavaDoc e)
137         {
138             //errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("lo053"));
139
servlet.log("[ERROR] TskAction at final catch: " + e.getMessage());
140             e.printStackTrace();
141
142         }
143
144         // Default if everthing else fails
145
return (mapping.findForward("showAddress"));
146
147     }
148 }
149
150
Popular Tags