JSP学习笔记(六):自定义标签-JSP2.x
<span style="font-family:Arial;">package taglib.jsp_two; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * * @Title: 继承 SimpleTagSupport Jsp2.0标签 * @Description: * @Copyright: Copyright (c) 2015 * @Company: * * @author: SAM-SHO * @version: 1.0 * @CreateDate:Feb 16, 2015 */ public class MultiTag extends SimpleTagSupport { private int num1; private int num2; @Override public void doTag() throws JspException, IOException { this.getJspContext().getOut().write("" + num1 + " * " + num2 + " = " + (num1 * num2)); } public int getNum1() { return num1; } public void setNum1(int num1) { this.num1 = num1; } public int getNum2() { return num2; } public void setNum2(int num2) { this.num2 = num2; } } // end</span>
<span style="font-family:Arial;"><tag> <name>multi</name> <tagclass>taglib.jsp_two.MultiTag</tagclass> <bodycontent>empty</bodycontent> <info>multi tag with parameters.</info> <attribute> <name>num1</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>num2</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag></span>
<span style="font-family:Arial;">package taglib.jsp_two; import java.io.IOException; import java.io.StringWriter; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * * @Title: 带标签体的jsp2.x实现 * @Description: * @Copyright: Copyright (c) 2015 * @Company: * * @author: SAM-SHO * @version: 1.0 * @CreateDate:Feb 16, 2015 */ public class ToUpperCaseTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { // 将 标签体内容读入该 writer StringWriter writer = new StringWriter(); // 标签体 为 JspFragment 的形式 JspFragment jspFragment = this.getJspBody(); // 通过 invoke 输出到指定的 writer 中。 // 如果参数为 null,将输出到默认的 writer 中,即 getJspContext().getOut() 输出到HTML中 jspFragment.invoke(writer); String content = writer.getBuffer().toString(); this.getJspContext().getOut().print(content.toUpperCase()); } } // end </span>
<span style="font-family:Arial;"> <tag> <name>toUpperCase</name> <tagclass>taglib.jsp_two.ToUpperCaseTag</tagclass> <bodycontent>tagdependent</bodycontent> <info>body tag</info> </tag></span>
<span style="font-family:Arial;">package taglib.jsp_two; import java.io.IOException; import java.io.StringWriter; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * * @Title: 带标多个标签体的jsp2.x实现 * @Description: * @Copyright: Copyright (c) 2015 * @Company: * * @author: SAM-SHO * @version: 1.0 * @CreateDate:Feb 16, 2015 */ public class MultiAttributeTag extends SimpleTagSupport { private JspFragment body1;//一个标签体 private JspFragment body2;//第二个标签体 public void setBody1(JspFragment body1) { this.body1 = body1; } public void setBody2(JspFragment body2) { this.body2 = body2; } @Override public void doTag() throws JspException, IOException { StringWriter writer1 = new StringWriter(); StringWriter writer2 = new StringWriter(); for (int i = 0; i < 5; i++) { // body1 调用 5 次 body1.invoke(writer1); } for (int i = 0; i < 3; i++) { // body2 调用 3 次 body2.invoke(writer2); } this.getJspContext().getOut().print("3 次调用 body2 后的结果:" + writer2.getBuffer().toString() + "<br/><br/>"); this.getJspContext().getOut().print("5 次调用 body1 后的结果:" + writer1.getBuffer().toString() + "<br/><br/>"); } } // end </span>
<span style="font-family:Arial;"><tag> <name>multiAttribute</name> <tagclass>taglib.jsp_two.MultiAttributeTag</tagclass> <bodycontent>tagdependent</bodycontent> <info>multi attribute tag with parameters.</info> <attribute> <name>body1</name> <required>false</required> <fragment>true</fragment> </attribute> <attribute> <name>body2</name> <required>false</required> <fragment>true</fragment> </attribute> </tag></span>
五、调用标签的JSP代码
<span style="font-family:Arial;"><%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://www.mayTaglib.com/tags" prefix="myTaglib"%> <c:set var="base" value="${pageContext.request.contextPath}" /> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <myTaglib:multi num1="3" num2="4" /> <myTaglib:toUpperCase>abcdefg</myTaglib:toUpperCase> <br/> <myTaglib:multiAttribute> <jsp:attribute name="body1">标签体一, </jsp:attribute> <jsp:attribute name="body2">标签体二, </jsp:attribute> </myTaglib:multiAttribute> </body> </html></span>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。