KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > request > ParameterValuesTag


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

16
17 package org.apache.taglibs.request;
18
19 import java.util.*;
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22 import javax.servlet.jsp.*;
23 import javax.servlet.jsp.tagext.*;
24
25 /**
26  * JSP Tag <b>parameterValues</b>, used to get HTTP parameter values for
27  * parameters which have multiple values
28  * using the standard JSP &lt;jsp:getProperty&gt; tag.
29  * <p>
30  * The script variable of name <b>id</b> is availble only within the
31  * body of the <b>parameterValues</b> tag.
32  * <p>
33  * Loops through all the parameter values for a parameter.
34  * <p>
35  * Must be nested within a <b>parameters</b> tag.
36  * <p>
37  * JSP Tag Lib Descriptor
38  * <p><pre>
39  * &lt;name&gt;parameterValues&lt;/name&gt;
40  * &lt;tagclass&gt;org.apache.taglibs.request.ParameterValuesTag&lt;/tagclass&gt;
41  * &lt;teiclass&gt;org.apache.taglibs.request.ParameterValuesTEI&lt;/teiclass&gt;
42  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
43  * &lt;info&gt;Used to get the name and values of a single parameter.&lt;/info&gt;
44  * &lt;attribute&gt;
45  * &lt;name&gt;id&lt;/name&gt;
46  * &lt;required&gt;true&lt;/required&gt;
47  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
48  * &lt;/attribute&gt;
49  * </pre>
50  *
51  * @author Glenn Nielsen
52  */

53
54 public class ParameterValuesTag extends BodyTagSupport
55 {
56     private String JavaDoc [] values = null;
57     private String JavaDoc value = null;
58     private int parameter_num = 0;
59
60     /**
61      * Gets the parameter values for the current parameter.
62      *
63      * @return SKIP_BODY if no parameter values exist, EVAL_BODY_TAG if parameter values exist
64      */

65     public final int doStartTag() throws JspException
66     {
67         // Initialize variables
68
values = null;
69         value = null;
70         parameter_num = 0;
71
72     // Get the parent parameters tag
73
ParametersTag pt;
74     try {
75         pt = (ParametersTag)this.findAncestorWithClass(this,
76         Class.forName("org.apache.taglibs.request.ParametersTag"));
77     } catch(Exception JavaDoc e) {
78         return SKIP_BODY;
79     }
80
81     // Get the parameters
82
values = ((HttpServletRequest)pageContext.getRequest()).getParameterValues(pt.getName());
83     if( values == null || values.length == 0 )
84         return SKIP_BODY;
85
86     value = values[0];
87     if( value == null )
88         return SKIP_BODY;
89
90     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
91     return EVAL_BODY_TAG;
92     }
93
94     /**
95      * Method called at end of each parameterValues tag.
96      *
97      * @return EVAL_BODY_TAG if there is another parameter value, or SKIP_BODY if there are no more parameter values
98      */

99     public final int doAfterBody() throws JspException
100     {
101     parameter_num++;
102     // See if this is the last parameter value
103
if( parameter_num >= values.length )
104         return SKIP_BODY;
105     // There is another parameter value, so loop again
106
value = values[parameter_num];
107     if( value == null )
108         return SKIP_BODY;
109     return EVAL_BODY_TAG;
110     }
111
112     /**
113      * Method called at end of Tag
114      * @return EVAL_PAGE
115      */

116     public final int doEndTag() throws JspException
117     {
118         pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
119     try
120     {
121         if(bodyContent != null)
122         bodyContent.writeOut(bodyContent.getEnclosingWriter());
123     } catch(java.io.IOException JavaDoc e)
124     {
125         throw new JspException("IO Error: " + e.getMessage());
126     }
127     return EVAL_PAGE;
128     }
129
130     /**
131      * Returns the value of the parameter.
132      * <p>
133      * &lt;jsp:getProperty name=<i>"id"</i> property="value"/&gt;
134      *
135      * @return String - value of the parameter
136      */

137     public final String JavaDoc getValue()
138     {
139     return value;
140     }
141
142 }
143
Popular Tags