JSP Tutorials - Herong's Tutorial Examples - Version 4.03, by Dr. Herong Yang
Value Type Conversion Example - AttValueTag.java
This section provides a tutorial example on how JSP server determines which setter method to call and how to convert the attribute value to match the setter method parameter.
Here is an example of to show you how JSP tag extension facility is converting the attribute values to the tag's setter methods required data types.
The tag class:
/** * AttValueTag.java * Copyright (c) 2012, HerongYang.com, All Rights Reserved. */ package herong; import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class AttValueTag extends TagSupport { private long longValue = 0; private Long longObject = null; private double doubleValue = 0.0; private Double doubleObject = null; private boolean booleanValue = true; private Boolean booleanObject = null; private String stringObject = null; public void setLongValue(long val) { longValue = val; } public void setLongObject(Long obj) { longObject = obj; } public void setDoubleValue(double val) { doubleValue = val; } public void setDoubleObject(Double obj) { doubleObject = obj; } public void setBooleanValue(boolean val) { booleanValue = val; } public void setBooleanObject(Boolean obj) { booleanObject = obj; } public void setStringObject(String obj) { stringObject = obj; } public int doStartTag() { JspWriter out = pageContext.getOut(); try { out.println("longValue = "+longValue+"<br/>"); out.println("longObject = "+longObject+"<br/>"); out.println("doubleValue = "+doubleValue+"<br/>"); out.println("doubleObject = "+doubleObject+"<br/>"); out.println("booleanValue = "+booleanValue+"<br/>"); out.println("booleanObject = "+booleanObject+"<br/>"); out.println("stringObject = "+stringObject+"<br/>"); } catch (IOException e) { System.err.println(e.toString()); } return SKIP_BODY; } }
The TLD file:
<?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> </attribute> <attribute> <name>longObject</name> <required>false</required> </attribute> <attribute> <name>doubleValue</name> <required>false</required> </attribute> <attribute> <name>doubleObject</name> <required>false</required> </attribute> <attribute> <name>booleanValue</name> <required>false</required> </attribute> <attribute> <name>booleanObject</name> <required>false</required> </attribute> <attribute> <name>stringObject</name> <required>false</required> </attribute> </tag> </taglib>
The JSP file:
<?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"> <!-- AttValueTagTest.jspx - Copyright (c) 2012, HerongYang.com, All Rights Reserved. --> <jsp:directive.page contentType="text/html"/> <html><body> <hy:attValue longObject="1" longValue="2" doubleObject="1.1" doubleValue="2.2" booleanObject="true" booleanValue="false" stringObject="Hello world!"/> </body></html> </jsp:root>
The output:
longValue = 2 longObject = 1 doubleValue = 2.2 doubleObject = 1.1 booleanValue = false booleanObject = true stringObject = Hello world!
The Servlet class produced by Tomcat 7 server:
... _jspx_th_hy_005fattValue_005f0.setStringObject("Hello world!"); _jspx_th_hy_005fattValue_005f0.setBooleanValue(false); _jspx_th_hy_005fattValue_005f0.setBooleanObject( new java.lang.Boolean(true)); _jspx_th_hy_005fattValue_005f0.setDoubleValue(2.2); _jspx_th_hy_005fattValue_005f0.setDoubleObject( new java.lang.Double(1.1)); _jspx_th_hy_005fattValue_005f0.setLongValue(2l); _jspx_th_hy_005fattValue_005f0.setLongObject(new java.lang.Long(1l)); ...
I only copied statements that interact with the tag class out of the Servlet class here. Note that:
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