JSP Tutorials - Herong's Tutorial Examples - Version 4.03, by Dr. Herong Yang
Passing Expression as String Example
This section provides a tutorial example to accept custom tag attributes as strings and convert them into different types of Java objects using the ExpressionEvaluatorManager.evaluate() method.
In this tutorial example, I want to take tag attributes as strings. Then convert them into different type of objects in my tag class using the ExpressionEvaluatorManager.evaluate() method in org.apache.taglibs.standard.lang.support package.
Here is the tag class, AttObjectTag.java:
/** * AttObjectTag.java * Copyright (c) 2012, HerongYang.com, All Rights Reserved. */ package herong; import java.util.*; import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import org.apache.taglibs.standard.lang.support.*; public class AttObjectTag extends TagSupport { private String booleanAtt = null; private Boolean booleanObject = null; private String stringAtt = null; private String stringObject = null; private String mapAtt = null; private Map mapObject = null; public void setBooleanAtt(String att) { booleanAtt = att; try { booleanObject = (Boolean) ExpressionEvaluatorManager.evaluate( "booleanAtt", booleanAtt, java.lang.Boolean.class, this, pageContext); } catch (JspException e) { System.err.println(e.toString()); } } public void setStringAtt(String att) { stringAtt = att; try { stringObject = (String) ExpressionEvaluatorManager.evaluate( "stringAtt", stringAtt, java.lang.String.class, this, pageContext); } catch (JspException e) { System.err.println(e.toString()); } } public void setMapAtt(String att) { mapAtt = att; try { mapObject = (Map) ExpressionEvaluatorManager.evaluate( "mapAtt", mapAtt, java.util.Map.class, this, pageContext); } catch (JspException e) { System.err.println(e.toString()); } } public int doStartTag() { JspWriter out = pageContext.getOut(); try { out.println("booleanAtt = "+booleanAtt+"<br/>"); out.println("booleanObject = "+booleanObject+"<br/>"); out.println("stringAtt = "+stringAtt+"<br/>"); out.println("stringObject = "+stringObject+"<br/>"); out.println("mapAtt = "+mapAtt+"<br/>"); if (mapObject!=null) { out.println("mapObject.size = "+mapObject.size()+"<br/>"); } else { out.println("mapObject = null<br/>"); } } catch (IOException e) { System.err.println(e.toString()); } return SKIP_BODY; } }
When compiling this tag class, I need to download and install Apache JSTL 1.1 standard library, jakarta-taglibs-standard-1.1.0.zip from http://tomcat.apache.org/taglibs/standard/.
Unzip jakarta-taglibs-standard-1.1.0.zip. Copy .\jakarta-taglibs-standard-1.1.0\lib\standard.jar to the Tomcat 7 server at C:\local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\lib and use it to compile the tag class:
C:\>\Progra~1\java\jdk1.7.0_07\bin\javac -classpath \local\apache-tomcat-7.0.32\lib\jsp-api.jar; \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\lib\standard.jar AttObjectTag.java C:\>copy AttObjectTag.class \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes\herong
Of course, the new must defined in the TLD file at \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\tlds\HyTaglib.tld:
<?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>attObject</name> <tag-class>herong.AttObjectTag</tag-class> <body-content>empty</body-content> <attribute> <name>booleanAtt</name> <required>false</required> </attribute> <attribute> <name>stringAtt</name> <required>false</required> </attribute> <attribute> <name>mapAtt</name> <required>false</required> </attribute> </tag> </taglib>
Don't forget to restart the Tomcat 7 server.
Noew create the JSP test page:
<?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"> <!-- AttObjectTagTest.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 booleanAtt="${false}" stringAtt="${'Herong Yang'}"/> <p>Expressions:</p> <hy:attObject booleanAtt="${1==1}" stringAtt="${pageContext.request.method}" mapAtt="${cookie}"/> </body></html> </jsp:root>
Push the JSP to the Tomcat 7 server and visit it with a Web browser. Surprisingly, I am getting a JasperException error!
HTTP Status 500 - /AttObjectTagTest.jspx (line: 12, column: 67) According to TLD or attribute directive in tag file, attribute stringAtt does not accept any expressions type Exception report description The server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: /AttObjectTagTest.jspx (line: 12, column: 67) According to TLD or attribute directive in tag file, attribute stringAtt does not accept any expressions org.apache.jasper.compiler.DefaultErrorHandler.jspError() org.apache.jasper.compiler.ErrorDispatcher.dispatch() org.apache.jasper.compiler.ErrorDispatcher.jspError() org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes() org.apache.jasper.compiler.Validator$ValidateVisitor.visit() org.apache.jasper.compiler.Node$CustomTag.accept() org.apache.jasper.compiler.Node$Nodes.visit() org.apache.jasper.compiler.Node$Visitor.visitBody() org.apache.jasper.compiler.Validator$ValidateVisitor.visit() org.apache.jasper.compiler.Node$UninterpretedTag.accept() org.apache.jasper.compiler.Node$Nodes.visit() org.apache.jasper.compiler.Node$Visitor.visitBody() org.apache.jasper.compiler.Validator$ValidateVisitor.visit() org.apache.jasper.compiler.Node$UninterpretedTag.accept() org.apache.jasper.compiler.Node$Nodes.visit() org.apache.jasper.compiler.Node$Visitor.visitBody() org.apache.jasper.compiler.Validator$ValidateVisitor.visit() org.apache.jasper.compiler.Node$JspRoot.accept() org.apache.jasper.compiler.Node$Nodes.visit() org.apache.jasper.compiler.Node$Visitor.visitBody() org.apache.jasper.compiler.Node$Visitor.visit() org.apache.jasper.compiler.Node$Root.accept() org.apache.jasper.compiler.Node$Nodes.visit() org.apache.jasper.compiler.Validator.validateExDirectives() org.apache.jasper.compiler.Compiler.generateJava() org.apache.jasper.compiler.Compiler.compile() org.apache.jasper.compiler.Compiler.compile() org.apache.jasper.compiler.Compiler.compile() org.apache.jasper.JspCompilationContext.compile() org.apache.jasper.servlet.JspServletWrapper.service() org.apache.jasper.servlet.JspServlet.serviceJspFile() org.apache.jasper.servlet.JspServlet.service() javax.servlet.http.HttpServlet.service()
The error message says that there is an expression at line 12, stringAtt="${'Herong Yang'}", but the "stringAtt" is not defined to accept any expression in the TLD file.
We have a problem here. If I turn on the "rtexprvalue" flag in the TLD file, my EL expressions will be evaluated by the JSP server. So how do I pass an EL expression as a string in an attribute and to be evaluated by my tag class, not by the JSP server? The answer should be using quotes to provide the EL expression as string literals. See next section for more details.
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