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.tiles.actions;
23
24 import java.io.IOException;
25 import java.io.PrintWriter;
26
27 import jakarta.servlet.http.HttpServletRequest;
28 import jakarta.servlet.http.HttpServletResponse;
29
30 import org.apache.struts.action.Action;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.tiles.ComponentDefinition;
35 import org.apache.struts.tiles.DefinitionsFactoryException;
36 import org.apache.struts.tiles.FactoryNotFoundException;
37 import org.apache.struts.tiles.NoSuchDefinitionException;
38 import org.apache.struts.tiles.TilesUtil;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public class DefinitionDispatcherAction extends Action {
72 private static final long serialVersionUID = 8663194696860741591L;
73
74
75
76
77 private transient final Logger log =
78 LoggerFactory.getLogger(DefinitionDispatcherAction.class);
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 @SuppressWarnings("deprecation")
95 public ActionForward execute(
96 ActionMapping mapping,
97 ActionForm form,
98 HttpServletRequest request,
99 HttpServletResponse response)
100 throws Exception {
101
102
103
104 String parameter = mapping.getParameter();
105 if (parameter == null) {
106 parameter = "def";
107 }
108
109
110 String name = request.getParameter(parameter);
111 if (name == null) {
112 log.error("Can't get parameter '{}'.", parameter);
113
114 return mapping.findForward("error");
115 }
116
117
118 try {
119
120 ComponentDefinition definition =
121 TilesUtil.getDefinition(
122 name,
123 request,
124 getServlet().getServletContext());
125
126 log.debug("Get Definition {}", definition);
127
128 org.apache.struts.tiles.DefinitionsUtil.setActionDefinition(request, definition);
129
130 } catch (FactoryNotFoundException e) {
131 log.error("Can't get definition factory.", e);
132 return mapping.findForward("error");
133
134 } catch (NoSuchDefinitionException e) {
135 log.error("Can't get definition '{}'.", name, e);
136 return mapping.findForward("error");
137
138 } catch (DefinitionsFactoryException e) {
139 log.error("General Factory error '{}'.", e.getMessage(), e);
140 return mapping.findForward("error");
141
142 } catch (Exception e) {
143 log.error("General error '{}'.", e.getMessage(), e);
144 return mapping.findForward("error");
145 }
146
147 return mapping.findForward("success");
148
149 }
150
151
152
153
154 @Deprecated
155 protected void printError(HttpServletResponse response, String msg)
156 throws IOException {
157 response.setContentType("text/plain");
158 PrintWriter writer = response.getWriter();
159 writer.println(msg);
160 writer.flush();
161 writer.close();
162 }
163 }