|
JSP Tag Attribute Handling
Part:
1
2
3
4
(Continued from previous part...)
Tag Attribute Value Expression Example - AttObjectTag.java
The tag class:
/**
* AttObjectTag.java
* Copyright (c) 2003 by Dr. Herong Yang. 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;
}
}
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>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>
The JSP page:
<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="1.2">
<!-- AttObjectTagTest.jsp
Copyright (c) 2003 by Dr. Herong Yang
-->
<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>
The 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
Note that:
- The JSTL expression evaluation tool is a static method, evaluate(), in
ExpressionEvaluationManager class, in org.apache.taglibs.standard.lang.support package.
- The expression rules are identical to those described in JSTL, because we
are using the same evaluation tool.
- If you click "refresh" button on the browser, the mapObject.size will be 1,
because the session id is stored in the cookie map.
Part:
1
2
3
4
|