JSP学习笔记(六):自定义标签-JSP2.x

一、SimpleTag 接口 和 SimpleTagSupport 类
SimpleTag 接口只有一个 doTag()方法,同时支持参数与标签体。一般实现 SimpleTagSupport 类即可。

二、简单实现
<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>



三、带标签体
1、实现方式不同: 
1)JSP1.x 标签将标签体通过 setBodyContent()方法注入 在 bodyTag 中,通过 getBodyContent()即可得到标签体。
2)SimpleTag 是通过 jspFragment 的对象实现的。

2、实现代码:

<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>


1)ToUpperCaseTag 中,标签体的内容被封装为 JspFragment 对象,通过 getJspBody 方法可以得到该对象。代码通过invoke() 方法输出到了指定的 StringWriter对象中,然后通过StringWriter 对象获取字符串。若果要直接输出,可以使用JspFragment.invoke(null) 。
2)tld注意:bodycontent为 tagdependent。

四、多个标签体
<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>


1、借助JSP的 <jsp:attribute/>行为。
2、两个标签体实际上就是标签类的两个属性。这两个属性必须是 JspFragment 类型。也必须通过 <jsp:attribute/>行为设定。而且tld 配置必须配置为 fragment类型。



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。