📄 tag-attribute.xtp
字号:
<s1 title="Tag Attributes"><p>Tags become more useful once you add attributes. This example createsa <var/ct:href/> tag.</p><p>With session rewriting, even browsers withoutcookies can support sessions. However, every <var/<a href>/> needs acall to <var/response.encodeURL()/>, which is annoying. With the<var/ct:href/> tag, you get that for free.</p><s2 title="Using an attribute taglib"><example title="href.jsp"><%@ taglib prefix="ct" uri="WEB-INF/tags.tld" %>Message: <ct:href href="test.jsp">test link</ct:href></example><results>Message: <a href="test.jsp;jsessionid=1234">test link</a></results></s2><s2 title="Defining the tag"><p>The href tag has a single attribute <var/href/>. Corresponding to theattribute <var/href/>, the HrefTag classimplements <var/getHref()/> and <var/setHref()/> methods.</p><example title="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; }}</example></s2><s2 title="Attribute Configuration"><p>Each expected attribute needs an entry in the .tld. In this case,<var/href/> is a required attribute, so the configuration needs toset <var/required/>. By setting <var/required/>, the JSP parser candetect errors at parse-time, making it easier to develop JSP pages withthe tag library.</p><example title="WEB-INF/tag.tld"><taglib> <tag> <name>href</name> <tagclass>test.HrefTag</tagclass> <attribute> <name>href</name> <required>true</required> </attribute> </tag></taglib></example></s2></s1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -