1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 * <servlet>
45 * <servlet-name>tiles</servlet-name>
46 * <servlet-class>org.apache.tiles.servlets.TilesServlet</servlet-class>
47 * <init-param>
48 * <param-name>definitions-config</param-name>
49 * <param-value>/WEB-INF/tiles-config.xml</param-value>
50 * </init-param>
51 * <load-on-startup>1</load-on-startup>
52 * </servlet>
53 * <!-- END SNIPPET: webxml -->
54 *
55 * <!-- START SNIPPET: strutsxml -->
56 * In struts.xml, use type="tiles" on your <result>.
57 *
58 * <action name="editUser" class="userAction" method="edit">
59 * <result name="success" type="tiles">userForm</result>
60 * <result name="input" type="tiles">userList</result>
61 * </action>
62 * <!-- END SNIPPET: strutsxml -->
63 *
64 *
65 * <!-- START SNIPPET: packageconfig -->
66 *
67 * Making this result type the default for the current package.
68 *
69 * <result-types>
70 * <result-type name="tiles"
71 * class="org.apache.struts2.views.tiles.TilesResult" default="true" />
72 * </result-types>
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 }