View Javadoc

1   /*
2    * $Id: TilesResult.java 651946 2008-04-27 13:41:38Z apetrelli $
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.struts2.views.tiles;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.struts2.ServletActionContext;
29  import org.apache.struts2.dispatcher.ServletDispatcherResult;
30  import org.apache.tiles.TilesContainer;
31  import org.apache.tiles.access.TilesAccess;
32  
33  import com.opensymphony.xwork2.ActionInvocation;
34  
35  /***
36   * <!-- START SNIPPET: description -->
37   * Renders a view using struts-tiles.
38   * <!-- END SNIPPET: description -->
39   *
40   * <!-- START SNIPPET: webxml -->
41   * In your web.xml file, you need to add a servlet entry for TilesServlet to load the tiles
42   * definitions into the ServletContext.
43   *
44   * &lt;servlet&gt;
45   *      &lt;servlet-name&gt;tiles&lt;/servlet-name&gt;
46   *      &lt;servlet-class&gt;org.apache.tiles.servlets.TilesServlet&lt;/servlet-class&gt;
47   *      &lt;init-param&gt;
48   *          &lt;param-name&gt;definitions-config&lt;/param-name&gt;
49   *          &lt;param-value&gt;/WEB-INF/tiles-config.xml&lt;/param-value&gt;
50   *      &lt;/init-param&gt;
51   *      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
52   * &lt;/servlet&gt;
53   * <!-- END SNIPPET: webxml -->
54   *
55   * <!-- START SNIPPET: strutsxml -->
56   * In struts.xml, use type="tiles" on your &lt;result&gt;.
57   *
58   * &lt;action name="editUser" class="userAction" method="edit"&gt;
59   *      &lt;result name="success" type="tiles"&gt;userForm&lt;/result&gt;
60   *      &lt;result name="input" type="tiles"&gt;userList&lt;/result&gt;
61   * &lt;/action&gt;
62   * <!-- END SNIPPET: strutsxml -->
63   *
64   *
65   * <!-- START SNIPPET: packageconfig -->
66   *
67   * Making this result type the default for the current package.
68   *
69   * &lt;result-types&gt;
70   *      &lt;result-type name="tiles"
71   * class="org.apache.struts2.views.tiles.TilesResult" default="true" /&gt;
72   * &lt;/result-types&gt;
73   * <!-- END SNIPPET: packageconfig -->
74   *
75   */
76  public class TilesResult extends ServletDispatcherResult {
77  
78      private static final long serialVersionUID = -3806939435493086244L;
79  
80      public TilesResult() {
81          super();
82      }
83  
84      public TilesResult(String location) {
85          super(location);
86      }
87      /***
88       * Dispatches to the given location. Does its forward via a RequestDispatcher. If the
89       * dispatch fails a 404 error will be sent back in the http response.
90       *
91       * @param location the location to dispatch to.
92       * @param invocation    the execution state of the action
93       * @throws Exception if an error occurs. If the dispatch fails the error will go back via the
94       *                   HTTP request.
95       */
96      public void doExecute(String location, ActionInvocation invocation) throws Exception {
97          setLocation(location);
98  
99          ServletContext servletContext = ServletActionContext.getServletContext();
100         TilesContainer container = TilesAccess.getContainer(servletContext);
101 
102         HttpServletRequest request = ServletActionContext.getRequest();
103         HttpServletResponse response = ServletActionContext.getResponse();
104 
105         container.render(location, request, response);
106     }
107 }