JSP Tutorials - Herong's Tutorial Examples - Version 4.03, by Dr. Herong Yang
Accepting EL Expressions
This section describes 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.
In the previous sections, I only talked about how to define an attribute in a custom tag, how attribute values are passed to the tag class, and how attribute values are converted to correct data type required by the tag class. Now let's look at the possibility of entering EL (Expression Langage) expressions as attribute values in a custom tag, similar to JSTL tags.
If you are using JSP 2.1, EL expressions are supported directly in custom tag attributes now. But if you are using JSP 1.2 or older versions, you need to use alternative approaches listed below.
1. Using EL expressions directly as attribute values in JSP 2.1. You need to define the attribute in the TLD file with <rtexprvalue>true</rtexprvalue>, then enter the attribute value with the following format:
<hy:tag att="${expression}"/>
2. Using Java expression elements as attribute values in JSP 1.2. You need to define the attribute in the TLD file with <rtexprvalue>true</rtexprvalue>, then enter the attribute value with the following format:
<hy:tag att="<%=java_expression%>"/>
3. Using EL expressions as strings and evaluate them inside the tag class in JSP 1.2. You can enter an EL expression in an attribute value, received it by the setter method as a string, then evaluate it to the desire data type using the expression evaluation tool offer by the Java EE.
The 1st approach is the best approach if you are running a JSP 2.1 server. An example is provided in the next section.
The 2nd approach requires the JSP page to be written in a non-XML format. And I don't like that format. So I am not going to try that.
The 3rd approach seems to be interesting and powerful. You can use it to pass an object of any class as an attribute value to the tag class. See the example later
Last update: 2012.
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat 7 Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
Non-ASCII Characters Support in JSP Pages
Overview of JSTL (JSP Standard Tag Libraries)
Tag Attributes Are Tag Object Properties
Setter Method Example - EchoTag.java
Value Type Conversion Example - AttValueTag.java
Accepting EL Expression Example
Passing Expression as String Example
Using jsp:attribute Action Elements