| |
Tags become more useful once you add attributes. This example creates
a ct:href tag.
With session rewriting, even browsers without
cookies can support sessions. However, every <a href> needs a
call to response.encodeURL(), which is annoying. With the
ct:href tag, you get that for free.
Using an attribute taglib |
href.jsp
<%@ taglib prefix="ct" uri="WEB-INF/tags.tld" %>
Message: <ct:href href="test.jsp">test link</ct:href>
|
Message: <a href="test.jsp;jsessionid=1234">test link</a>
|
The href tag has a single attribute href. Corresponding to the
attribute href, the HrefTag class
implements getHref() and setHref() methods.
HrefTag.java
package test;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HrefTag extends TagSupport {
private String href;
public String getHref()
{
return href;
}
public void setHref(String href)
{
this.href = href;
}
public int doStartTag() throws JspException
{
try {
JspWriter out = pageContext.getOut();
HttpServletResponse response;
response = (HttpServletResponse) pageContext.getResponse();
out.print("<a href=\"");
out.print(response.encodeURL(href));
out.print("\">");
} catch (IOException e) {
}
return EVAL_BODY;
}
public int doEndTag() throws JspException
{
try {
pageContext.getOut().print("</a>");
} catch (IOException e) {
}
return EVAL_PAGE;
}
}
|
Each expected attribute needs an entry in the .tld. In this case,
href is a required attribute, so the configuration needs to
set required. By setting required, the JSP parser can
detect errors at parse-time, making it easier to develop JSP pages with
the tag library.
WEB-INF/tag.tld
<taglib>
<tag>
<name>href</name>
<tagclass>test.HrefTag</tagclass>
<attribute>
<name>href</name>
<required>true</required>
</attribute>
</tag>
</taglib>
|
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark,
and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc. | |
|