View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts.webapp.dispatch;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import jakarta.servlet.http.HttpServletRequest;
27  import jakarta.servlet.http.HttpServletResponse;
28  
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  import org.apache.struts.action.ActionMessage;
33  import org.apache.struts.action.ActionMessages;
34  import org.apache.struts.extras.actions.LookupDispatchAction;
35  
36  /**
37   * Example LookupDispatchAction.
38   *
39   * @version $Rev$ $Date$
40   */
41  public class LookupDispatchExampleAction extends LookupDispatchAction {
42      private static final long serialVersionUID = 6423904873036854367L;
43  
44      private HashMap<String, String> keyMethodMap = new HashMap<>();
45      private int fooCount;
46      private int barCount;
47  
48      /**
49       * Constructor - populate the key method map.
50       */
51      public LookupDispatchExampleAction() {
52          keyMethodMap.put("button.foo.label", "doFoo");
53          keyMethodMap.put("button.bar.label", "doBar");
54      }
55  
56      /**
57       * Example "foo" method.
58       *
59       * @param mapping The ActionMapping used to select this instance
60       * @param form The optional ActionForm bean for this request
61       * @param request The servlet request we are processing
62       * @param response The servlet response we are creating
63       *
64       * @exception Exception if business logic throws an exception
65       */
66      public ActionForward doFoo(ActionMapping mapping,
67                                 ActionForm form,
68                                 HttpServletRequest request,
69                                 HttpServletResponse response)
70          throws Exception {
71  
72          fooCount++;
73  
74          ActionMessages messages = new ActionMessages();
75          messages.add("foo", new ActionMessage("count.foo.message", fooCount+""));
76          saveMessages(request, messages);
77  
78          return (mapping.findForward("success"));
79  
80      }
81  
82      /**
83       * Example "bar" method.
84       *
85       * @param mapping The ActionMapping used to select this instance
86       * @param form The optional ActionForm bean for this request
87       * @param request The servlet request we are processing
88       * @param response The servlet response we are creating
89       *
90       * @exception Exception if business logic throws an exception
91       */
92      public ActionForward doBar(ActionMapping mapping,
93                                 ActionForm form,
94                                 HttpServletRequest request,
95                                 HttpServletResponse response)
96          throws Exception {
97          barCount++;
98  
99          ActionMessages messages = new ActionMessages();
100         messages.add("bar", new ActionMessage("count.bar.message", barCount+""));
101         saveMessages(request, messages);
102 
103         return (mapping.findForward("success"));
104 
105     }
106 
107     /**
108      * Provides the mapping from resource key to method name.
109      *
110      * @return Resource key / method name map.
111      */
112     protected Map<String, String> getKeyMethodMap() {
113         return keyMethodMap;
114     }
115 }