|
JSP Tag Attribute Handling
Part:
1
2
3
4
(Continued from previous part...)
Tag Attribute Value Type Conversion
In my previous example, the attribute value is passed to the tag object property
as a string data type. Can we pass attribute values to tag object as other data types
or objects? The answer is yes. JSP tag extension facility will try to convert attribute
values to the data types of the tag object properties. I couldn't find any official
specifications on the match rules. The following is my guesses:
- Match the attribute name to the setter method of a property with the same name
in the tag object.
- If the data type of the input parameter of the setter method is a primitive
data type, like "long",
give the attribute value as literal data to the input parameter.
- If the data type of the input parameter of the setter method is the wrapper class
of a primitive data type, like "Long",
instantiate an object of that class with the attribute value
as a String input to the constructor, and give the new object to the input parameter.
- If the data type of the input parameter of the setter method is the String class,
give the attribute value as string literal to the input parameter.
- If the data type of the input parameter of the setter method is any other class,
unknown behavior.
Tag Attribute Value Type Conversion Example - AttValueTag.java
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) 2003 by Dr. Herong Yang. 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) 2003 by Dr. Herong Yang
-->
<taglib>
<tlib-version>1</tlib-version>
<jsp-version>1.2</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>
(Continued on next part...)
Part:
1
2
3
4
|