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 * Perform a forward or redirect to a page that is looked up in the
35 * configuration information associated with our application.
36 *
37 * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
38 * $
39 */
40 public class ForwardTag extends TagSupport {
41
42
43 /**
44 * The message resources for this package.
45 */
46 protected static MessageResources messages =
47 MessageResources.getMessageResources(
48 "org.apache.struts.taglib.logic.LocalStrings");
49
50 /**
51 * The logical name of the <code>ActionForward</code> entry to be looked
52 * up.
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 * Defer processing until the end of this tag is encountered.
68 *
69 * @throws JspException if a JSP exception has occurred
70 */
71 public int doStartTag() throws JspException {
72 return (SKIP_BODY);
73 }
74
75 /**
76 * Look up the ActionForward associated with the specified name, and
77 * perform a forward or redirect to that path as indicated.
78 *
79 * @throws JspException if a JSP exception has occurred
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 * Forward to the given path converting exceptions to JspException.
116 *
117 * @param path The path to forward to.
118 * @throws JspException
119 * @since Struts 1.2
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 * Redirect to the given path converting exceptions to JspException.
134 *
135 * @param path The path to redirect to.
136 * @throws JspException
137 * @since Struts 1.2
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 * Release all allocated resources.
162 */
163 public void release() {
164 super.release();
165 name = null;
166 }
167 }