View Javadoc

1   /*
2    * $Id: StrutsTag.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.config.ModuleConfig;
21  import org.apache.struts.taglib.TagUtils;
22  import org.apache.struts.util.MessageResources;
23  
24  import javax.servlet.jsp.JspException;
25  import javax.servlet.jsp.tagext.TagSupport;
26  
27  /***
28   * Define a scripting variable that exposes the requested Struts internal
29   * configuraton object.
30   *
31   * @version $Rev: 376840 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
32   *          $
33   */
34  public class StrutsTag extends TagSupport {
35      /***
36       * The message resources for this package.
37       */
38      protected static MessageResources messages =
39          MessageResources.getMessageResources(
40              "org.apache.struts.taglib.bean.LocalStrings");
41  
42      // ------------------------------------------------------------- Properties
43  
44      /***
45       * The name of the scripting variable that will be exposed as a page scope
46       * attribute.
47       */
48      protected String id = null;
49  
50      /***
51       * The name of the <code>ActionFormBean</code> object to be exposed.
52       */
53      protected String formBean = null;
54  
55      /***
56       * The name of the <code>ActionForward</code> object to be exposed.
57       */
58      protected String forward = null;
59  
60      /***
61       * The name of the <code>ActionMapping</code> object to be exposed.
62       */
63      protected String mapping = null;
64  
65      public String getId() {
66          return (this.id);
67      }
68  
69      public void setId(String id) {
70          this.id = id;
71      }
72  
73      public String getFormBean() {
74          return (this.formBean);
75      }
76  
77      public void setFormBean(String formBean) {
78          this.formBean = formBean;
79      }
80  
81      public String getForward() {
82          return (this.forward);
83      }
84  
85      public void setForward(String forward) {
86          this.forward = forward;
87      }
88  
89      public String getMapping() {
90          return (this.mapping);
91      }
92  
93      public void setMapping(String mapping) {
94          this.mapping = mapping;
95      }
96  
97      // --------------------------------------------------------- Public Methods
98  
99      /***
100      * Retrieve the required configuration object and expose it as a scripting
101      * variable.
102      *
103      * @throws JspException if a JSP exception has occurred
104      */
105     public int doStartTag() throws JspException {
106         // Validate the selector arguments
107         int n = 0;
108 
109         if (formBean != null) {
110             n++;
111         }
112 
113         if (forward != null) {
114             n++;
115         }
116 
117         if (mapping != null) {
118             n++;
119         }
120 
121         if (n != 1) {
122             JspException e =
123                 new JspException(messages.getMessage("struts.selector"));
124 
125             TagUtils.getInstance().saveException(pageContext, e);
126             throw e;
127         }
128 
129         // Retrieve our module configuration information
130         ModuleConfig config =
131             TagUtils.getInstance().getModuleConfig(pageContext);
132 
133         // Retrieve the requested object to be exposed
134         Object object = null;
135         String selector = null;
136 
137         if (formBean != null) {
138             selector = formBean;
139             object = config.findFormBeanConfig(formBean);
140         } else if (forward != null) {
141             selector = forward;
142             object = config.findForwardConfig(forward);
143         } else if (mapping != null) {
144             selector = mapping;
145             object = config.findActionConfig(mapping);
146         }
147 
148         if (object == null) {
149             JspException e =
150                 new JspException(messages.getMessage("struts.missing", selector));
151 
152             TagUtils.getInstance().saveException(pageContext, e);
153             throw e;
154         }
155 
156         // Expose this value as a scripting variable
157         pageContext.setAttribute(id, object);
158 
159         return (SKIP_BODY);
160     }
161 
162     /***
163      * Release all allocated resources.
164      */
165     public void release() {
166         super.release();
167         id = null;
168         formBean = null;
169         forward = null;
170         mapping = null;
171     }
172 }