JSP Tutorials - Herong's Tutorial Examples - Version 4.03, by Dr. Herong Yang
Using jsp:attribute Action Elements
This section provides a tutorial example on how to use JSP 'attribute' action elements to specify attribute values to custom tags as XML element body contents instead of XML attribute values.
Since EL (Expression Language) expressions are supported by default in static text and custom tag attributes directly in JSP 2.1, any string that looks like an EL expression, ${...}, will be processed as EL expression by the JSP server.
So how how I stop the JSP 2.1 server processing booleanAtt="${false}" as an EL expression? I tried 3 different approaches and 1 of them works:
1. Put the expression in single quotes as shown below. But it does not work, :-(. It is because the JSP server still considers a quoted string as an expression, an literal expression.
<hy:attObject booleanAtt="'${false}'" stringAtt="'${\'Herong Yang\'}'"/>
2. Escape the ($) sign as (\$) as shown below. But it does not work, :-(. Somehow the JSP server still gives the JasperException error saying "According to TLD or attribute directive in tag file, attribute booleanAtt does not accept any expressions".
<hy:attObject booleanAtt="\${false}" stringAtt="\${'Herong Yang'}"/>
3. Use JSP "attribute" action element to provide the attribute value as shown below. It works, :-)! The JSP server is happy now, because I am not providing EL expressions as XML attribute values. They are provided as body contents of the <jsp:attribute> elements.
<hy:attObject> <jsp:attribute name="booleanAtt">\${false}</jsp:attribute> <jsp:attribute name="stringAtt">\${'Herong Yang'}</jsp:attribute> </hy:attObject>
Now I can revise my test JSP page as: AttObjectTagTestRevised.jspx using JSP "attribute" action elements to pass EL expressions as strings to my tab class:
<?xml version="1.0"?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="1.2"> <!-- AttObjectTagTestRevised.jspx - Copyright (c) 2012, HerongYang.com, All Rights Reserved. --> <jsp:directive.page contentType="text/html"/> <html><body> <p>Regular Strings:</p> <hy:attObject booleanAtt="true" stringAtt="Hello world!"/> <p>Literals:</p> <hy:attObject> <jsp:attribute name="booleanAtt">\${false}</jsp:attribute> <jsp:attribute name="stringAtt">\${'Herong Yang'}</jsp:attribute> </hy:attObject> <p>Expressions:</p> <hy:attObject> <jsp:attribute name="booleanAtt">\${1==1}</jsp:attribute> <jsp:attribute name="stringAtt"> \${pageContext.request.method} </jsp:attribute> <jsp:attribute name="mapAtt">\${cookie}</jsp:attribute> </hy:attObject> </body></html> </jsp:root>
Visit this revised JSP page and I get this output:
Regular Strings: booleanAtt = true booleanObject = true stringAtt = Hello world! stringObject = Hello world! mapAtt = null mapObject = null Literals: booleanAtt = ${false} booleanObject = false stringAtt = ${'Herong Yang'} stringObject = Herong Yang mapAtt = null mapObject = null Expressions: booleanAtt = ${1==1} booleanObject = true stringAtt = ${pageContext.request.method} stringObject = GET mapAtt = ${cookie} mapObject.size = 0
The output is perfectly correct. For example, (mapAtt = ${cookie}) proves that the attribute value is received as a string. (mapObject.size = 0) proves that (${cookie}) is evaluated to a Map object correctly.
If you click the "refresh" button on the browser, the mapObject.size will become 1, because the session id is stored in the cookie Map in the subsequent request:
Regular Strings: booleanAtt = true booleanObject = true stringAtt = Hello world! stringObject = Hello world! mapAtt = null mapObject = null Literals: booleanAtt = ${false} booleanObject = false stringAtt = ${'Herong Yang'} stringObject = Herong Yang mapAtt = ${cookie} mapObject.size = 1 Expressions: booleanAtt = ${1==1} booleanObject = true stringAtt = ${pageContext.request.method} stringObject = GET mapAtt = ${cookie} mapObject.size = 1
But there is new problem. (mapAtt = ${cookie}) is displayed in the "Literals" section! The "mapAtt" attribute value should be null, because I am not providing any value for "mapAtt" for that "hy:attObject" tag.
What's wrong with the Tomcat 7 JSP server? Anybody knows why?
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