View Javadoc

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