1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.taglib.logic;
19
20 import org.apache.struts.action.ActionForward;
21 import org.apache.struts.config.ModuleConfig;
22 import org.apache.struts.taglib.TagUtils;
23 import org.apache.struts.util.MessageResources;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import javax.servlet.jsp.JspException;
28 import javax.servlet.jsp.tagext.TagSupport;
29
30 /***
31 * Perform a forward or redirect to a page that is looked up in the
32 * configuration information associated with our application.
33 *
34 * @version $Rev: 376842 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
35 * $
36 */
37 public class ForwardTag extends TagSupport {
38
39
40 /***
41 * The message resources for this package.
42 */
43 protected static MessageResources messages =
44 MessageResources.getMessageResources(
45 "org.apache.struts.taglib.logic.LocalStrings");
46
47 /***
48 * The logical name of the <code>ActionForward</code> entry to be looked
49 * up.
50 */
51 protected String name = null;
52
53 public String getName() {
54 return (this.name);
55 }
56
57 public void setName(String name) {
58 this.name = name;
59 }
60
61
62
63 /***
64 * Defer processing until the end of this tag is encountered.
65 *
66 * @throws JspException if a JSP exception has occurred
67 */
68 public int doStartTag() throws JspException {
69 return (SKIP_BODY);
70 }
71
72 /***
73 * Look up the ActionForward associated with the specified name, and
74 * perform a forward or redirect to that path as indicated.
75 *
76 * @throws JspException if a JSP exception has occurred
77 */
78 public int doEndTag() throws JspException {
79
80 ActionForward forward = null;
81 ModuleConfig config =
82 TagUtils.getInstance().getModuleConfig(pageContext);
83
84 if (config != null) {
85 forward = (ActionForward) config.findForwardConfig(name);
86 }
87
88 if (forward == null) {
89 JspException e =
90 new JspException(messages.getMessage("forward.lookup", name));
91
92 TagUtils.getInstance().saveException(pageContext, e);
93 throw e;
94 }
95
96
97 String path = forward.getPath();
98
99 path = config.getPrefix() + path;
100
101 if (forward.getRedirect()) {
102 this.doRedirect(path);
103 } else {
104 this.doForward(path);
105 }
106
107
108 return (SKIP_PAGE);
109 }
110
111 /***
112 * Forward to the given path converting exceptions to JspException.
113 *
114 * @param path The path to forward to.
115 * @throws JspException
116 * @since Struts 1.2
117 */
118 protected void doForward(String path)
119 throws JspException {
120 try {
121 pageContext.forward(path);
122 } catch (Exception e) {
123 TagUtils.getInstance().saveException(pageContext, e);
124 throw new JspException(messages.getMessage("forward.forward", name,
125 e.toString()));
126 }
127 }
128
129 /***
130 * Redirect to the given path converting exceptions to JspException.
131 *
132 * @param path The path to redirect to.
133 * @throws JspException
134 * @since Struts 1.2
135 */
136 protected void doRedirect(String path)
137 throws JspException {
138 HttpServletRequest request =
139 (HttpServletRequest) pageContext.getRequest();
140
141 HttpServletResponse response =
142 (HttpServletResponse) pageContext.getResponse();
143
144 try {
145 if (path.startsWith("/")) {
146 path = request.getContextPath() + path;
147 }
148
149 response.sendRedirect(response.encodeRedirectURL(path));
150 } catch (Exception e) {
151 TagUtils.getInstance().saveException(pageContext, e);
152 throw new JspException(messages.getMessage("forward.redirect",
153 name, e.toString()));
154 }
155 }
156
157 /***
158 * Release all allocated resources.
159 */
160 public void release() {
161 super.release();
162 name = null;
163 }
164 }