javax.faces.webapp.UIComponentELTag Maven / Gradle / Ivy
Show all versions of javax.faces-api Show documentation
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package javax.faces.webapp;
import javax.el.ELException;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
/**
* {@link UIComponentELTag} specializes its superclass to allow for
* properties that take their values from EL API expressions.
*
* This tag is designed for use with Faces version 1.2 and JSP
* version 2.1 containers.
*
*/
public abstract class UIComponentELTag extends UIComponentClassicTagBase implements Tag {
// ------------------------------------------------------------- Attributes
/**
* The value binding expression (if any) used to wire up this component
* to a {@link UIComponent} property of a JavaBean class.
*/
private ValueExpression binding = null;
/**
* Set the value expression for our component.
*
* @param binding The new value expression
*
* @throws JspException if an error occurs
*/
public void setBinding(ValueExpression binding) throws JspException {
this.binding = binding;
}
@Override
protected boolean hasBinding() {
return null != binding;
}
/**
* An override for the rendered attribute associated with our
* {@link UIComponent}.
*/
private ValueExpression rendered = null;
/**
* Set an override for the rendered attribute.
*
* @param rendered The new value for rendered attribute
*/
public void setRendered(ValueExpression rendered) {
this.rendered = rendered;
}
/**
* Return the {@link ELContext} for the {@link FacesContext} for
* this request.
*
* This is a convenience for
* getFacesContext().getELContext()
.
*/
@Override
protected ELContext getELContext() {
FacesContext fc = getFacesContext();
ELContext result = null;
if (null != fc) {
result = fc.getELContext();
}
return result;
}
// ------------------------------------------------------------ Tag Methods
/**
* Release any resources allocated during the execution of this
* tag handler.
*/
@Override
public void release() {
this.binding = null;
this.rendered = null;
super.release();
}
// ------------------------------------------------------- Protected Methods
/**
* Override properties and attributes of the specified component,
* if the corresponding properties of this tag handler instance were
* explicitly set. This method must be called ONLY
* if the specified {@link UIComponent} was in fact created during
* the execution of this tag handler instance, and this call will occur
* BEFORE the {@link UIComponent} is added to
* the view.
*
* Tag subclasses that want to support additional set properties
* must ensure that the base class setProperties()
* method is still called. A typical implementation that supports
* extra properties foo
and bar
would look
* something like this:
*
* protected void setProperties(UIComponent component) {
* super.setProperties(component);
* if (foo != null) {
* component.setAttribute("foo", foo);
* }
* if (bar != null) {
* component.setAttribute("bar", bar);
* }
* }
*
*
* The default implementation overrides the following properties:
*
* rendered
- Set if a value for the
* rendered
property is specified for
* this tag handler instance.
* rendererType
- Set if the getRendererType()
* method returns a non-null value.
*
*
* @param component {@link UIComponent} whose properties are to be
* overridden
*/
@Override
protected void setProperties(UIComponent component) {
// The "id" property is explicitly set when components are created
// so it does not need to be set here
if (rendered != null) {
if (rendered.isLiteralText()) {
try {
component.setRendered(Boolean.valueOf(rendered.getExpressionString())
.booleanValue());
} catch (ELException e) {
throw new FacesException(e);
}
} else {
component.setValueExpression("rendered", rendered);
}
}
if (getRendererType() != null) {
component.setRendererType(getRendererType());
}
}
/**
* Create and return a new child component of the type returned by
* calling getComponentType()
. If this {@link UIComponentELTag}
* has a non-null binding
attribute, this is done by
* call {@link Application#createComponent} with the {@link ValueExpression}
* created for the binding
attribute, and the
* {@link ValueExpression} will be stored on the component. Otherwise,
* {@link Application#createComponent} is called with only
* the component type. Finally, initialize the components id
* and other properties.
*
* @param context {@link FacesContext} for the current request
* @param newId id of the component
*/
@Override
protected UIComponent createComponent(FacesContext context, String newId) throws JspException {
UIComponent component;
Application application = context.getApplication();
if (binding != null) {
component = application.createComponent(binding, context,
getComponentType());
component.setValueExpression("binding", binding);
} else {
component = application.createComponent(getComponentType());
}
component.setId(newId);
setProperties(component);
return component;
}
}