日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JSP2.0中Simple Tag介绍

發(fā)布時間:2023/12/10 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JSP2.0中Simple Tag介绍 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JSP2.0中為了簡化標簽的復雜性,增加了制作Simple Tag的標簽類SimpleTagSupport類。
SimpleTagSupport類是實現(xiàn)SimpleTag接口的。它只需要實現(xiàn)一個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;//重復的次數(shù)
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">
重復執(zhí)行 ....<br>
</jsp:attribute>
</mytag:Repeat>
</body>
</html>

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

DynamicAttributes接口
只要制作的標簽實現(xiàn)了DynamicAttributes接口就有動態(tài)屬性的功能。
例如:我們要做多個數(shù)的累加運算,則AddSimpleTag標簽就可以通過實現(xiàn)DynamicAttributes接口就可以實現(xiàn)了.

實現(xiàn)DynamicAttributes接口,必須實現(xiàn)setDynamicAttributes()方法,此方法用來接收動態(tài)屬性.

舉例:第一步:制作標簽處理類

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 {

//用來接收動態(tài)屬性
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類是實現(xiàn)SimpleTag接口的。它只需要實現(xiàn)一個doTag()方法即可,而不需要一堆回傳值。

轉載于:https://www.cnblogs.com/jadmin/archive/2007/08/14/2206335.html

總結

以上是生活随笔為你收集整理的JSP2.0中Simple Tag介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。