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, if
a string contains another string, and if the current user belongs to
a 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 22-2 shows the tag handler class for
<ora:ifUserInRole>
, which takes advantage of
this JSTL support class.
Example 22-2. Tag handler for a conditional action
package com.ora.jsp.tags; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.jstl.core.*; ...
Get JavaServer Pages, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.