View Javadoc

1   /*
2    * $Id: PageTag.java 376840 2006-02-10 21:00:51Z 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.bean;
19  
20  import org.apache.struts.taglib.TagUtils;
21  import org.apache.struts.util.MessageResources;
22  
23  import javax.servlet.jsp.JspException;
24  import javax.servlet.jsp.tagext.TagSupport;
25  
26  /***
27   * Define a scripting variable that exposes the requested page context item as
28   * a scripting variable and a page scope bean.
29   *
30   * @version $Rev: 376840 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
31   *          $
32   */
33  public class PageTag extends TagSupport {
34      /***
35       * The message resources for this package.
36       */
37      protected static MessageResources messages =
38          MessageResources.getMessageResources(
39              "org.apache.struts.taglib.bean.LocalStrings");
40  
41      // ------------------------------------------------------------- Properties
42  
43      /***
44       * The name of the scripting variable that will be exposed as a page scope
45       * attribute.
46       */
47      protected String id = null;
48  
49      /***
50       * The name of the page context property to be retrieved.
51       */
52      protected String property = null;
53  
54      public String getId() {
55          return (this.id);
56      }
57  
58      public void setId(String id) {
59          this.id = id;
60      }
61  
62      public String getProperty() {
63          return (this.property);
64      }
65  
66      public void setProperty(String property) {
67          this.property = property;
68      }
69  
70      // --------------------------------------------------------- Public Methods
71  
72      /***
73       * Retrieve the required configuration object and expose it as a scripting
74       * variable.
75       *
76       * @throws JspException if a JSP exception has occurred
77       */
78      public int doStartTag() throws JspException {
79          // Retrieve the requested object to be exposed
80          Object object = null;
81  
82          if ("application".equalsIgnoreCase(property)) {
83              object = pageContext.getServletContext();
84          } else if ("config".equalsIgnoreCase(property)) {
85              object = pageContext.getServletConfig();
86          } else if ("request".equalsIgnoreCase(property)) {
87              object = pageContext.getRequest();
88          } else if ("response".equalsIgnoreCase(property)) {
89              object = pageContext.getResponse();
90          } else if ("session".equalsIgnoreCase(property)) {
91              object = pageContext.getSession();
92          } else {
93              JspException e =
94                  new JspException(messages.getMessage("page.selector", property));
95  
96              TagUtils.getInstance().saveException(pageContext, e);
97              throw e;
98          }
99  
100         // Expose this value as a scripting variable
101         pageContext.setAttribute(id, object);
102 
103         return (SKIP_BODY);
104     }
105 
106     /***
107      * Release all allocated resources.
108      */
109     public void release() {
110         super.release();
111         id = null;
112         property = null;
113     }
114 }