attributestag.java
来自「jakarta-taglibs」· Java 代码 · 共 159 行
JAVA
159 行
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.taglibs.application;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* JSP Tag <b>attributes</b>, used to get ServletContext attribute
* information using the standard JSP 1.2 <jsp:getProperty> tag.
* <p>
* The script variable of name <b>id</b> is availble only within the
* body of the <b>attributes</b> tag.
* <p>
* Loops through all the attributes in the ServletContext.
* <p>
* JSP Tag Lib Descriptor
* <p><pre>
* <name>attributes</name>
* <tagclass>org.apache.taglibs.application.AttributesTag</tagclass>
* <teiclass>org.apache.taglibs.application.AttributesTEI</teiclass>
* <bodycontent>JSP</bodycontent>
* <info>Loop through all attributes or get a single attribute.</info>
* <attribute>
* <name>id</name>
* <required>true</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* </pre>
*
* @author Morgan Delagrange
*/
public class AttributesTag extends BodyTagSupport
{
private String name = null;
private ServletContext app = null;
private Enumeration attributes = null;
private String attribute = null;
/**
* Loops through all the attributes that came within the ServletContext.
*
* @return SKIP_BODY if no attributes are found, EVAL_BODY_TAG if an attribute exists
*/
public final int doStartTag() throws JspException
{
// Get the ServletContext
app = pageContext.getServletContext();
attribute = null;
if( name != null ) {
if( app.getAttribute(name) != null) {
attribute = name;
}
} else {
attributes = app.getAttributeNames();
if( attributes == null || !attributes.hasMoreElements() )
return SKIP_BODY;
attribute = (String)attributes.nextElement();
}
if( attribute == null )
return SKIP_BODY;
pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
return EVAL_BODY_TAG;
}
/**
* Method called at end of each attributes tag.
*
* @return EVAL_BODY_TAG if there is another attribute, or SKIP_BODY if there are no more attributes
*/
public final int doAfterBody() throws JspException
{
// See if this is the last attribute
if( name != null || !attributes.hasMoreElements() )
return SKIP_BODY;
// There is another attribute, so loop again
attribute = (String)attributes.nextElement();
if( attribute == null )
return SKIP_BODY;
return EVAL_BODY_TAG;
}
/**
* Method called at end of Tag
* @return EVAL_PAGE
*/
public final int doEndTag() throws JspException
{
pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
try
{
if(bodyContent != null)
bodyContent.writeOut(bodyContent.getEnclosingWriter());
} catch(java.io.IOException e)
{
throw new JspException("IO Error: " + e.getMessage());
}
return EVAL_PAGE;
}
/**
* Returns the name of the attribute.
* <p>
* <jsp:getProperty name=<i>"id"</i> property="name"/>
*
* @return String - attribute name
*/
public final String getName()
{
return attribute;
}
/**
* Set the name of the attribute to get
*
* @param String - attribute name
*/
public final void setName(String name)
{
this.name = name;
}
/**
* Returns the value of the attribute.
* <p>
* <jsp:getProperty name=<i>"id"</i> property="value"/>
*
* @return String - value of the attribute
*/
public final String getValue()
{
Object value = app.getAttribute(attribute);
if( value == null )
return "";
return "" + value.toString();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?