1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.taglib.logic;
22
23 import org.apache.struts.action.ActionForward;
24 import org.apache.struts.config.ModuleConfig;
25 import org.apache.struts.taglib.TagUtils;
26 import org.apache.struts.util.MessageResources;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.servlet.jsp.JspException;
31 import javax.servlet.jsp.tagext.TagSupport;
32
33
34
35
36
37
38
39
40 public class ForwardTag extends TagSupport {
41
42
43
44
45
46 protected static MessageResources messages =
47 MessageResources.getMessageResources(
48 "org.apache.struts.taglib.logic.LocalStrings");
49
50
51
52
53
54 protected String name = null;
55
56 public String getName() {
57 return (this.name);
58 }
59
60 public void setName(String name) {
61 this.name = name;
62 }
63
64
65
66
67
68
69
70
71 public int doStartTag() throws JspException {
72 return (SKIP_BODY);
73 }
74
75
76
77
78
79
80
81 public int doEndTag() throws JspException {
82
83 ActionForward forward = null;
84 ModuleConfig config =
85 TagUtils.getInstance().getModuleConfig(pageContext);
86
87 if (config != null) {
88 forward = (ActionForward) config.findForwardConfig(name);
89 }
90
91 if (forward == null) {
92 JspException e =
93 new JspException(messages.getMessage("forward.lookup", name));
94
95 TagUtils.getInstance().saveException(pageContext, e);
96 throw e;
97 }
98
99
100 String path = forward.getPath();
101
102 path = config.getPrefix() + path;
103
104 if (forward.getRedirect()) {
105 this.doRedirect(path);
106 } else {
107 this.doForward(path);
108 }
109
110
111 return (SKIP_PAGE);
112 }
113
114
115
116
117
118
119
120
121 protected void doForward(String path)
122 throws JspException {
123 try {
124 pageContext.forward(path);
125 } catch (Exception e) {
126 TagUtils.getInstance().saveException(pageContext, e);
127 throw new JspException(messages.getMessage("forward.forward", name,
128 e.toString()));
129 }
130 }
131
132
133
134
135
136
137
138
139 protected void doRedirect(String path)
140 throws JspException {
141 HttpServletRequest request =
142 (HttpServletRequest) pageContext.getRequest();
143
144 HttpServletResponse response =
145 (HttpServletResponse) pageContext.getResponse();
146
147 try {
148 if (path.startsWith("/")) {
149 path = request.getContextPath() + path;
150 }
151
152 response.sendRedirect(response.encodeRedirectURL(path));
153 } catch (Exception e) {
154 TagUtils.getInstance().saveException(pageContext, e);
155 throw new JspException(messages.getMessage("forward.redirect",
156 name, e.toString()));
157 }
158 }
159
160
161
162
163 public void release() {
164 super.release();
165 name = null;
166 }
167 }