KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > bean > StrutsTag


1 /*
2  * $Id: StrutsTag.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
20 package org.apache.struts.taglib.bean;
21
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 import org.apache.struts.config.ModuleConfig;
27 import org.apache.struts.taglib.TagUtils;
28 import org.apache.struts.util.MessageResources;
29
30
31 /**
32  * Define a scripting variable that exposes the requested Struts
33  * internal configuraton object.
34  *
35  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
36  */

37
38 public class StrutsTag extends TagSupport JavaDoc {
39
40
41     // ------------------------------------------------------------- Properties
42

43
44     /**
45      * The name of the scripting variable that will be exposed as a page
46      * scope attribute.
47      */

48     protected String JavaDoc id = null;
49
50     public String JavaDoc getId() {
51         return (this.id);
52     }
53
54     public void setId(String JavaDoc id) {
55         this.id = id;
56     }
57
58
59     /**
60      * The message resources for this package.
61      */

62     protected static MessageResources messages =
63         MessageResources.getMessageResources
64         ("org.apache.struts.taglib.bean.LocalStrings");
65
66
67     /**
68      * The name of the <code>ActionFormBean</code> object to be exposed.
69      */

70     protected String JavaDoc formBean = null;
71
72     public String JavaDoc getFormBean() {
73         return (this.formBean);
74     }
75
76     public void setFormBean(String JavaDoc formBean) {
77         this.formBean = formBean;
78     }
79
80
81     /**
82      * The name of the <code>ActionForward</code> object to be exposed.
83      */

84     protected String JavaDoc forward = null;
85
86     public String JavaDoc getForward() {
87         return (this.forward);
88     }
89
90     public void setForward(String JavaDoc forward) {
91         this.forward = forward;
92     }
93
94
95     /**
96      * The name of the <code>ActionMapping</code> object to be exposed.
97      */

98     protected String JavaDoc mapping = null;
99
100     public String JavaDoc getMapping() {
101         return (this.mapping);
102     }
103
104     public void setMapping(String JavaDoc mapping) {
105         this.mapping = mapping;
106     }
107
108
109     // --------------------------------------------------------- Public Methods
110

111
112     /**
113      * Retrieve the required configuration object and expose it as a
114      * scripting variable.
115      *
116      * @exception JspException if a JSP exception has occurred
117      */

118     public int doStartTag() throws JspException JavaDoc {
119
120         // Validate the selector arguments
121
int n = 0;
122         if (formBean != null)
123             n++;
124         if (forward != null)
125             n++;
126         if (mapping != null)
127             n++;
128         if (n != 1) {
129             JspException JavaDoc e = new JspException JavaDoc
130                 (messages.getMessage("struts.selector"));
131             TagUtils.getInstance().saveException(pageContext, e);
132             throw e;
133         }
134
135         // Retrieve our module configuration information
136
ModuleConfig config = TagUtils.getInstance().getModuleConfig(pageContext);
137
138         // Retrieve the requested object to be exposed
139
Object JavaDoc object = null;
140         String JavaDoc selector = null;
141         if (formBean != null) {
142             selector = formBean;
143             object = config.findFormBeanConfig(formBean);
144         } else if (forward != null) {
145             selector = forward;
146             object = config.findForwardConfig(forward);
147         } else if (mapping != null) {
148             selector = mapping;
149             object = config.findActionConfig(mapping);
150         }
151         if (object == null) {
152             JspException JavaDoc e = new JspException JavaDoc
153                 (messages.getMessage("struts.missing", selector));
154             TagUtils.getInstance().saveException(pageContext, e);
155             throw e;
156         }
157
158         // Expose this value as a scripting variable
159
pageContext.setAttribute(id, object);
160         return (SKIP_BODY);
161
162     }
163
164
165     /**
166      * Release all allocated resources.
167      */

168     public void release() {
169
170         super.release();
171         id = null;
172         formBean = null;
173         forward = null;
174         mapping = null;
175
176     }
177
178
179 }
180
Popular Tags