View Javadoc

1   /*
2    * $Id: ForwardTag.java 376842 2006-02-10 21:02:03Z husted $
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  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      // ----------------------------------------------------------- Properties
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      // ------------------------------------------------------- Public Methods
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          // Look up the desired ActionForward entry
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          // Forward or redirect to the corresponding actual path
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         // Skip the remainder of this page
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 }