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  
22  package org.apache.struts.tiles2.actions;
23  
24  import org.apache.struts.action.Action;
25  import org.apache.struts.action.ActionForm;
26  import org.apache.struts.action.ActionForward;
27  import org.apache.struts.action.ActionMapping;
28  import org.apache.tiles.AttributeContext;
29  import org.apache.tiles.TilesContainer;
30  import org.apache.tiles.access.TilesAccess;
31  import org.apache.tiles.request.ApplicationContext;
32  import org.apache.tiles.request.Request;
33  import org.apache.tiles.request.jakarta.servlet.ServletRequest;
34  import org.apache.tiles.request.jakarta.servlet.ServletUtil;
35  
36  import jakarta.servlet.ServletException;
37  import jakarta.servlet.http.HttpServletRequest;
38  import jakarta.servlet.http.HttpServletResponse;
39  
40  /**
41   * Base class for Tiles Actions.
42   * This class has the same role as Struts Action. It provides a method execute(...)
43   * called when action is invoked. The difference is, that the execute() method takes
44   * an additional parameter : tile context.
45   * This class extends Struts Action. Subclasses should override
46   * execute(AttributeContext ...) method instead of Struts
47   * execute(ActionMapping ...) method.
48   * @version $Rev$ $Date$
49   */
50  public abstract class TilesAction extends Action {
51      private static final long serialVersionUID = -565671066052461589L;
52  
53      /**
54       * Original Struts Action's method.
55       * Retrieve current Tile context and call TilesAction execute method.
56       * Do not overload this method!
57       *
58       * @param mapping The ActionMapping used to select this instance.
59       * @param form The optional ActionForm bean for this request (if any).
60       * @param req The HTTP request we are processing.
61       * @param res The HTTP response we are creating.
62       *
63       * @throws Exception if the application business logic throws
64       *  an exception
65       * @return The forward object.
66       * @since Struts 1.1
67       */
68      public ActionForward execute(
69          ActionMapping mapping,
70          ActionForm form,
71          HttpServletRequest req,
72          HttpServletResponse res)
73          throws Exception {
74  
75          // Try to retrieve tile context
76          ApplicationContext applicationContext = ServletUtil
77                  .getApplicationContext(req.getSession().getServletContext());
78          Request request = new ServletRequest(applicationContext,
79                  req, res);
80          TilesContainer container = TilesAccess.getContainer(applicationContext);
81          AttributeContext context = container.getAttributeContext(request);
82          if (context == null) {
83              throw new ServletException(
84                  "Can't find Tile context for '"
85                      + this.getClass().getName()
86                      + "'. TilesAction subclasses must be called from a Tile");
87          }
88  
89          return this.execute(context, mapping, form, req, res);
90      }
91  
92      /**
93       * Process the specified HTTP request and create the corresponding HTTP
94       * response (or forward to another web component that will create it),
95       * with provision for handling exceptions thrown by the business logic.
96       * <br>
97       * Override this method to provide functionality.
98       *
99       * @param context The current Tile context, containing Tile attributes.
100      * @param mapping The ActionMapping used to select this instance.
101      * @param form The optional ActionForm bean for this request (if any).
102      * @param request The HTTP request we are processing.
103      * @param response The HTTP response we are creating.
104      *
105      * @throws Exception if the application business logic throws
106      *  an exception
107      * @return The forward object.
108      * @since Struts 1.1
109      */
110     public ActionForward execute(
111         AttributeContext context,
112         ActionMapping mapping,
113         ActionForm form,
114         HttpServletRequest request,
115         HttpServletResponse response)
116         throws Exception {
117 
118         return null;
119     }
120 }