JSP2.0中Simple Tag介绍(更加多的例子)

JSP2.0中为了简化标签的复杂性,增加了制作Simple Tag的标签类SimpleTagSupport类。
SimpleTagSupport类是实现SimpleTag接口的。它只需要实现一个doTag()方法即可,而不需要一堆回传值。

举例说明:
例1:HelloSimpleTag标签
第一步:制作标签处理类
HelloSimpleTag.java

package com.newould.taglib;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class HelloSimpleTag extends SimpleTagSupport {

public void doTag() throws JspException, IOException {
  
      JspWriter out = getJspContext().getOut();
      out.println("Hello Simple Tag");
}
}

第二步:编写标签性质文件
MyTaglib.tld

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
       version="2.0">
    
       <description>My Taglib by JavaWorld.com.tw</description>
       <tlib-version>1.0</tlib-version>
       <jsp-version>2.0</jsp-version>
       <short-name>Mytaglib</short-name>
       <uri></uri>
......

<tag>
         <description>Hello Simple Tag</description>
         <name>HelloSimpleTag</name>
         <tag-class>com.newould.taglib.HelloSimpleTag</tag-class>
         <body-content>empty</body-content>
       </tag>
</taglib>

第三步:编写Jsp网页
HelloSimpleTag.jsp

<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib uri="/WEB-INF/tlds/MyTaglib.tld" prefix="mytag" %>

<html>
<head>
<title>HelloSimpleTag.jsp</title>
</head>
<body>

<h2>Simple Tag 标签</h2>

<h1><mytag:HelloSimpleTag /></h1>

</body>
</html>

=================================================================

例2:AddSimpleTag标签
第一步:制作标签处理类
AddSimpleTag.java

package com.newould.taglib;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class AddSimpleTag extends SimpleTagSupport {

private int num1 = 0; 
private int num2 = 0;

public void setNum1(int num1) {
      this.num1 = num1;
}

public void setNum2(int num2) {
      this.num2 = num2;
}

public void doTag() throws JspException, IOException {

      JspContext ctx = getJspContext();
      JspWriter out = ctx.getOut();
   
      int sum = num1 + num2;
      ctx.setAttribute("sum", Integer.toString(sum));
  
      out.println(num1 + " + " + num2 + " = " + sum);
}
}

第二步:编写标签性质文件
MyTaglib.tld

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
       version="2.0">
    
       <description>My Taglib by JavaWorld.com.tw</description>
       <tlib-version>1.0</tlib-version>
       <jsp-version>2.0</jsp-version>
       <short-name>Mytaglib</short-name>
       <uri></uri>
......

<tag>
         <description>Add Simple Tag</description>
         <name>Add</name>
         <tag-class>com.newould.taglib.AddSimpleTag</tag-class>
         <body-content>empty</body-content>
      
         <attribute>
           <name>num1</name>
           <required>true</required>
           <rtexprvalue>true</rtexprvalue>
         </attribute>

         <attribute>
           <name>num2</name>
           <required>true</required>
           <rtexprvalue>true</rtexprvalue>
         </attribute>
            
       </tag>
   
</taglib>

第三步:编写Jsp网页
AddSimpleTag.jsp

%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib uri="/WEB-INF/tlds/MyTaglib.tld" prefix="mytag" %>

<html>
<head>
<title>AddSimpleTag.jsp</title>
</head>
<body>

<h2>AddSimpleTag 标签</h2>

<h1><mytag:Add num1="5" num2="9" /></h1>

最后结果:${sum}

</body>
</html>

=================================================================

例3 RepeatSimpleTag标签 
RepeatSimpleTag标签 主要是用来重复显示某段文字。
这个例子在处理上与前两个例子有点不同

第一步:制作标签处理类
RepeatSimpleTag.java

package com.newould.taglib;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class RepeatSimpleTag extends SimpleTagSupport {

private int count = 0;//重复的次数
private JspFragment fragment;//重复的内容

public void setCount(int count) {
      this.count = count;
}

public void setFragment(JspFragment fragment) {
      this.fragment = fragment;
}

public void doTag() throws JspException, IOException {

      JspContext ctx = getJspContext();
      JspWriter out = ctx.getOut();
   
      for(int i=0 ; i<count ; i++) {
       fragment.invoke(null);//表示将fragment的内容显示出来
      }
}
}

第二步:编写标签性质文件
MyTaglib.tld

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
       version="2.0">
    
       <description>My Taglib by JavaWorld.com.tw</description>
       <tlib-version>1.0</tlib-version>
       <jsp-version>2.0</jsp-version>
       <short-name>Mytaglib</short-name>
       <uri></uri>
......

<tag>
         <description>Repeate Simple Tag</description>
         <name>Repeat</name>
         <tag-class>com.newould.taglib.RepeatSimpleTag</tag-class>
         <body-content>empty</body-content>
      
         <attribute>
           <name>count</name>
           <required>true</required>
           <rtexprvalue>true</rtexprvalue>
         </attribute>

         <attribute>
           <name>fragment</name>
           <required>true</required>
           <fragment>true</fragment>
         </attribute> 
       </tag>    
   
</taglib>

注意:<fragment>true</fragment>,一定要这样设定fragment属性。

第三步:编写Jsp网页
RepeatSimpleTag.jsp

<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib uri="/WEB-INF/tlds/MyTaglib.tld" prefix="mytag" %>

<html>
<head>
<title>RepeatSimpleTag.jsp</title>
</head>
<body>

<h2>RepeatSimpleTag 标签</h2>

<mytag:Repeat count="5" >
<jsp:attribute name="fragment">
重复执行 ....<br>
</jsp:attribute>
</mytag:Repeat>
</body>
</html>

=================================================================

DynamicAttributes接口
只要制作的标签实现了DynamicAttributes接口就有动态属性的功能。
例如:我们要做多个数的累加运算,则AddSimpleTag标签就可以通过实现DynamicAttributes接口就可以实现了.

实现DynamicAttributes接口,必须实现setDynamicAttributes()方法,此方法用来接收动态属性.

举例:第一步:制作标签处理类

package com.newould.taglib;

import java.io.*;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class DynamicAdd extends SimpleTagSupport implements DynamicAttributes {

//用来接收动态属性
private ArrayList keys = new ArrayList();
private ArrayList values = new ArrayList();

public void doTag() throws JspException, IOException {

      JspContext ctx = getJspContext();
      JspWriter out = ctx.getOut();
   
      float num = 0;
      float sum = Float.parseFloat((String)values.get(0));
      out.print(sum); 
   
      for (int i = 1 ; i < keys.size() ; i++) {
       String temp = (String)values.get(i);
       num = Float.parseFloat(temp);
       sum = sum + num; 
       out.print(" + " + num);
      }
   
      out.print(" = " + sum);
      ctx.setAttribute("sum", Float.toString(sum));
   
}

public void setDynamicAttribute(String uri, String name, Object value) throws JspException {
      keys.add(name);
      values.add(value);
}
}

第二步:编写标签性质文件
<tag>
         <description>DynamicAttribute</description>
         <name>DynAdd</name>
         <tag-class>com.newould.taglib.DynamicAdd</tag-class>
         <body-content>empty</body-content>

         <dynamic-attributes>true</dynamic-attributes>
       </tag>    

第三步:编写Jsp网页
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib prefix="JSPBook" tagdir="/WEB-INF/tags/" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
<title>CH16 - DynAdd.jsp</title>
</head>
<body>

<h2>Tag File 范例</h2>

<JSPBook:DynAdd num1="111" num2="222" num3="444" >

<jsp:attribute name="great">
      <font color="red">SUM:${sum} ...</red>
</jsp:attribute>
<jsp:attribute name="less">
      <font color="blue">SUM:${sum} ...</red>
</jsp:attribute>
</JSPBook:DynAdd>

</body>
</html> 
JSP2.0中为了简化标签的复杂性,增加了制作Simple Tag的标签类SimpleTagSupport类。
SimpleTagSupport类是实现SimpleTag接口的。它只需要实现一个doTag()方法即可,而不需要一堆回传值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值