KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > logic > ForwardTag


1 /*
2  * $Id: ForwardTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.taglib.logic;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 import org.apache.struts.action.ActionForward;
27 import org.apache.struts.config.ModuleConfig;
28 import org.apache.struts.taglib.TagUtils;
29 import org.apache.struts.util.MessageResources;
30
31 /**
32  * Perform a forward or redirect to a page that is looked up in the
33  * configuration information associated with our application.
34  *
35  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
36  */

37 public class ForwardTag extends TagSupport JavaDoc {
38
39     // ----------------------------------------------------------- Properties
40

41     /**
42      * The message resources for this package.
43      */

44     protected static MessageResources messages =
45         MessageResources.getMessageResources(
46             "org.apache.struts.taglib.logic.LocalStrings");
47
48     /**
49      * The logical name of the <code>ActionForward</code> entry to be
50      * looked up.
51      */

52     protected String JavaDoc name = null;
53
54     public String JavaDoc getName() {
55         return (this.name);
56     }
57
58     public void setName(String JavaDoc name) {
59         this.name = name;
60     }
61
62     // ------------------------------------------------------- Public Methods
63

64     /**
65      * Defer processing until the end of this tag is encountered.
66      *
67      * @exception JspException if a JSP exception has occurred
68      */

69     public int doStartTag() throws JspException JavaDoc {
70
71         return (SKIP_BODY);
72
73     }
74
75     /**
76      * Look up the ActionForward associated with the specified name,
77      * and perform a forward or redirect to that path as indicated.
78      *
79      * @exception JspException if a JSP exception has occurred
80      */

81     public int doEndTag() throws JspException JavaDoc {
82
83         // Look up the desired ActionForward entry
84
ActionForward forward = null;
85         ModuleConfig config = TagUtils.getInstance().getModuleConfig(pageContext);
86         
87         if (config != null){
88             forward = (ActionForward) config.findForwardConfig(name);
89         }
90             
91         if (forward == null) {
92             JspException JavaDoc e =
93                 new JspException JavaDoc(messages.getMessage("forward.lookup", name));
94             TagUtils.getInstance().saveException(pageContext, e);
95             throw e;
96         }
97
98         // Forward or redirect to the corresponding actual path
99
String JavaDoc path = forward.getPath();
100         path = config.getPrefix() + path;
101
102         if (forward.getRedirect()) {
103             this.doRedirect(path);
104         } else {
105             this.doForward(path);
106         }
107
108         // Skip the remainder of this page
109
return (SKIP_PAGE);
110
111     }
112
113     /**
114      * Forward to the given path converting exceptions to JspException.
115      * @param path The path to forward to.
116      * @throws JspException
117      * @since Struts 1.2
118      */

119     protected void doForward(String JavaDoc path) throws JspException JavaDoc {
120         try {
121             pageContext.forward(path);
122             
123         } catch (Exception JavaDoc e) {
124             TagUtils.getInstance().saveException(pageContext, e);
125             throw new JspException JavaDoc(
126                 messages.getMessage("forward.forward", name, e.toString()));
127         }
128     }
129
130     /**
131      * Redirect to the given path converting exceptions to JspException.
132      * @param path The path to redirect to.
133      * @throws JspException
134      * @since Struts 1.2
135      */

136     protected void doRedirect(String JavaDoc path) throws JspException JavaDoc {
137         HttpServletRequest JavaDoc request =
138             (HttpServletRequest JavaDoc) pageContext.getRequest();
139             
140         HttpServletResponse JavaDoc response =
141             (HttpServletResponse JavaDoc) pageContext.getResponse();
142             
143         try {
144             if (path.startsWith("/")) {
145                 path = request.getContextPath() + path;
146             }
147             
148             response.sendRedirect(response.encodeRedirectURL(path));
149             
150         } catch (Exception JavaDoc e) {
151             TagUtils.getInstance().saveException(pageContext, e);
152             throw new JspException JavaDoc(
153                 messages.getMessage("forward.redirect", name, e.toString()));
154         }
155     }
156
157     /**
158      * Release all allocated resources.
159      */

160     public void release() {
161
162         super.release();
163         name = null;
164
165     }
166
167 }
168
Popular Tags