001: /*
002: * $Id: ParameterTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.bean;
022:
023: import org.apache.struts.taglib.TagUtils;
024: import org.apache.struts.util.MessageResources;
025:
026: import javax.servlet.jsp.JspException;
027: import javax.servlet.jsp.tagext.TagSupport;
028:
029: /**
030: * Define a scripting variable based on the value(s) of the specified
031: * parameter received with this request.
032: *
033: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
034: * $
035: */
036: public class ParameterTag extends TagSupport {
037: /**
038: * The message resources for this package.
039: */
040: protected static MessageResources messages = MessageResources
041: .getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
042:
043: // ------------------------------------------------------------- Properties
044:
045: /**
046: * The name of the scripting variable that will be exposed as a page scope
047: * attribute.
048: */
049: protected String id = null;
050:
051: /**
052: * Return an array of parameter values if <code>multiple</code> is
053: * non-null.
054: */
055: protected String multiple = null;
056:
057: /**
058: * The name of the parameter whose value is to be exposed.
059: */
060: protected String name = null;
061:
062: /**
063: * The default value to return if no parameter of the specified name is
064: * found.
065: */
066: protected String value = null;
067:
068: public String getId() {
069: return (this .id);
070: }
071:
072: public void setId(String id) {
073: this .id = id;
074: }
075:
076: public String getMultiple() {
077: return (this .multiple);
078: }
079:
080: public void setMultiple(String multiple) {
081: this .multiple = multiple;
082: }
083:
084: public String getName() {
085: return (this .name);
086: }
087:
088: public void setName(String name) {
089: this .name = name;
090: }
091:
092: public String getValue() {
093: return (this .value);
094: }
095:
096: public void setValue(String value) {
097: this .value = value;
098: }
099:
100: // --------------------------------------------------------- Public Methods
101:
102: /**
103: * Retrieve the required property and expose it as a scripting variable.
104: *
105: * @throws JspException if a JSP exception has occurred
106: */
107: public int doStartTag() throws JspException {
108: // Deal with a single parameter value
109: if (multiple == null) {
110: String value = pageContext.getRequest().getParameter(name);
111:
112: if ((value == null) && (this .value != null)) {
113: value = this .value;
114: }
115:
116: if (value == null) {
117: JspException e = new JspException(messages.getMessage(
118: "parameter.get", name));
119:
120: TagUtils.getInstance().saveException(pageContext, e);
121: throw e;
122: }
123:
124: pageContext.setAttribute(id, value);
125:
126: return (SKIP_BODY);
127: }
128:
129: // Deal with multiple parameter values
130: String[] values = pageContext.getRequest().getParameterValues(
131: name);
132:
133: if ((values == null) || (values.length == 0)) {
134: if (this .value != null) {
135: values = new String[] { this .value };
136: }
137: }
138:
139: if ((values == null) || (values.length == 0)) {
140: JspException e = new JspException(messages.getMessage(
141: "parameter.get", name));
142:
143: TagUtils.getInstance().saveException(pageContext, e);
144: throw e;
145: }
146:
147: pageContext.setAttribute(id, values);
148:
149: return (SKIP_BODY);
150: }
151:
152: /**
153: * Release all allocated resources.
154: */
155: public void release() {
156: super.release();
157: id = null;
158: multiple = null;
159: name = null;
160: value = null;
161: }
162: }
|