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.struts.tiles2.preparer;
23
24 import java.io.IOException;
25
26 import org.apache.tiles.AttributeContext;
27 import org.apache.tiles.preparer.PreparerException;
28 import org.apache.tiles.preparer.ViewPreparer;
29 import org.apache.tiles.request.Request;
30 import org.apache.tiles.request.jakarta.servlet.ServletRequest;
31
32 import jakarta.servlet.RequestDispatcher;
33 import jakarta.servlet.ServletException;
34 import jakarta.servlet.http.HttpServletRequest;
35 import jakarta.servlet.http.HttpServletResponse;
36
37
38
39
40 public class UrlPreparer implements ViewPreparer {
41
42
43
44
45 private String url;
46
47
48
49
50
51
52 public UrlPreparer(String url) {
53 this.url = url;
54 }
55
56
57 public void execute(Request tilesContext,
58 AttributeContext attributeContext) throws PreparerException {
59
60 if (tilesContext instanceof ServletRequest) {
61 ServletRequest servletTilesContext =
62 (ServletRequest) tilesContext;
63 HttpServletRequest request = servletTilesContext.getRequest();
64 HttpServletResponse response = servletTilesContext.getResponse();
65 RequestDispatcher rd = request.getSession().getServletContext()
66 .getRequestDispatcher(url);
67 if (rd == null) {
68 throw new PreparerException(
69 "Controller can't find url '" + url + "'.");
70 }
71
72 try {
73 rd.include(request, response);
74 } catch (ServletException e) {
75 throw new PreparerException(
76 "The request dispatcher threw an exception", e);
77 } catch (IOException e) {
78 throw new PreparerException(
79 "The request dispatcher threw an I/O exception", e);
80 }
81 } else {
82 throw new PreparerException("Cannot dispatch url '" + url
83 + "' since this preparer has not been called under a servlet environment");
84 }
85 }
86
87 }