001: /*
002: * $Id: PageTag.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 that exposes the requested page context item as
031: * a scripting variable and a page scope bean.
032: *
033: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
034: * $
035: */
036: public class PageTag 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: * The name of the page context property to be retrieved.
053: */
054: protected String property = null;
055:
056: public String getId() {
057: return (this .id);
058: }
059:
060: public void setId(String id) {
061: this .id = id;
062: }
063:
064: public String getProperty() {
065: return (this .property);
066: }
067:
068: public void setProperty(String property) {
069: this .property = property;
070: }
071:
072: // --------------------------------------------------------- Public Methods
073:
074: /**
075: * Retrieve the required configuration object and expose it as a scripting
076: * variable.
077: *
078: * @throws JspException if a JSP exception has occurred
079: */
080: public int doStartTag() throws JspException {
081: // Retrieve the requested object to be exposed
082: Object object = null;
083:
084: if ("application".equalsIgnoreCase(property)) {
085: object = pageContext.getServletContext();
086: } else if ("config".equalsIgnoreCase(property)) {
087: object = pageContext.getServletConfig();
088: } else if ("request".equalsIgnoreCase(property)) {
089: object = pageContext.getRequest();
090: } else if ("response".equalsIgnoreCase(property)) {
091: object = pageContext.getResponse();
092: } else if ("session".equalsIgnoreCase(property)) {
093: object = pageContext.getSession();
094: } else {
095: JspException e = new JspException(messages.getMessage(
096: "page.selector", property));
097:
098: TagUtils.getInstance().saveException(pageContext, e);
099: throw e;
100: }
101:
102: // Expose this value as a scripting variable
103: pageContext.setAttribute(id, object);
104:
105: return (SKIP_BODY);
106: }
107:
108: /**
109: * Release all allocated resources.
110: */
111: public void release() {
112: super.release();
113: id = null;
114: property = null;
115: }
116: }
|