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