Integrating Custom Conditional Actions
The JSTL core library
contains one, generic
conditional action: <c:if>. This action
handles all conditions that can be expressed as Boolean EL
expressions, but you often need more than that. Examples from Part II
of this book include: testing if a mail address has valid syntax and
if the current user belongs to a specific group.
To help developing this type of conditional custom action, JSTL
includes a base class called
javax.servlet.jsp.jstl.core.ConditionalTagSupport
:
public abstract class ConditionalTagSupport extends javax.servlet.jsp.tagext.TagSupport
It contains the following public methods:
protected abstract boolean condition( ) throws JspTagException public void setVar(String var) public void setScope(String scope) public int doStartTag( ) throws JspException public void release( )
The doStartTag( ) implementation calls the
condition( ) method and takes care of saving the
result if the var and scope
attributes are set.
By extending this class and providing an implementation of the
condition( ) and setter methods for all attributes
you need, you get a conditional action that is consistent with the
semantics of the JSTL version.
Example 23-1 shows the tag handler class for
<ora:ifUserInRole>, which takes advantage of
this JSTL support class.
package com.ora.jsp.tags; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.jstl.core.*; import org.apache.taglibs.standard.lang.support.*; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access