Accepting EL Expression Example

This section provides a tutorial example to test how EL expressions can be used in custom tag attributes. In JSP 2.1 EL expressions can be used directly. But in JSP 1.2, you need to use alternative approaches.

As of JSP 2.1, EL (Expression Language) expressions can be used directly in custom tag attributes. Let me use the AttValueTag.java tag class presented earlier to show you an example.

First we need to modify my TLD file, HyTaglib.tld, to turn on the "rtexprvalue" flag on my tag attributes:

<?xml version="1.0"?>
<!DOCTYPE taglib PUBLIC 
 "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 
 "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_2.dtd"> 
<!-- HyTaglib.tld
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<taglib>
<tlib-version>1</tlib-version>
<jsp-version>2.1</jsp-version> 
<short-name>Herong's Tag Library</short-name> 
<tag>
 <name>attValue</name>
 <tag-class>herong.AttValueTag</tag-class>
 <body-content>empty</body-content>
 <attribute>
  <name>longValue</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
 <attribute>
  <name>longObject</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
 <attribute>
  <name>doubleValue</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
 <attribute>
  <name>doubleObject</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
 <attribute>
  <name>booleanValue</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
 <attribute>
  <name>booleanObject</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
 <attribute>
  <name>stringObject</name>
  <required>false</required>
  <rtexprvalue>true</rtexprvalue>
 </attribute>
</tag>
</taglib>

Here is my test JSP page, AttValueTagElTest.jspx. All attribute values are using EL expressions.

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" 
   xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="2.1"> 
<!-- AttValueTagElTest.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<hy:attValue longObject="${3-2}" longValue="${1+1}"
 doubleObject="${1.1}" doubleValue="${2.2}"
 booleanObject="${2 gt 1}" booleanValue="${2 lt 1}" 
 stringObject="${'Hello world!'}"/>
</body></html>
</jsp:root>

Visit this JSP page, I am getting the same result as AttValueTagTest.jspx:

longValue = 2
longObject = 1
doubleValue = 2.2
doubleObject = 1.1
booleanValue = false
booleanObject = true
stringObject = Hello world!

Do you want to see how the JSP server converts those EL expressions into the Servlet class? Here are Java statements converted from my custom attribute values:

...
_jspx_th_hy_005fattValue_005f0.setStringObject(
   (java.lang.String) 
   org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(
   "${'Hello world!'}", java.lang.String.class, 
   (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
_jspx_th_hy_005fattValue_005f0.setBooleanValue(
   ((java.lang.Boolean) 
   org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(
   "${2 lt 1}", java.lang.Boolean.class, 
   (javax.servlet.jsp.PageContext)_jspx_page_context, null, false))
   .booleanValue());
_jspx_th_hy_005fattValue_005f0.setBooleanObject(
   (java.lang.Boolean) 
   org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(
   "${2 gt 1}", java.lang.Boolean.class, 
   (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
_jspx_th_hy_005fattValue_005f0.setDoubleValue(
   ((java.lang.Double) 
   org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(
   "${2.2}", java.lang.Double.class, 
   (javax.servlet.jsp.PageContext)_jspx_page_context, null, false))
   .doubleValue());
_jspx_th_hy_005fattValue_005f0.setDoubleObject(
   (java.lang.Double) 
   org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(
   "${1.1}", java.lang.Double.class, 
   (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
_jspx_th_hy_005fattValue_005f0.setLongValue(
   ((java.lang.Long) 
   org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(
   "${1+1}", java.lang.Long.class, 
   (javax.servlet.jsp.PageContext)_jspx_page_context, null, false))
   .longValue());
_jspx_th_hy_005fattValue_005f0.setLongObject(
   (java.lang.Long) 
   org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(
   "${3-2}", java.lang.Long.class, 
   (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
...

Note that:

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

Custom Tag Attributes

 Tag Attributes Are Tag Object Properties

 Setter Method Example - EchoTag.java

 Value Type Conversion

 Value Type Conversion Example - AttValueTag.java

 Accepting EL Expressions

Accepting EL Expression Example

 Passing Expression as String Example

 Using jsp:attribute Action Elements

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version