tag学习

aglib根目录节点:tlib-version 、short-name、uri

tag节点:name、tag-class、body-content

attribute节点:name、required、fragment

 

实例

package net.sx.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class SimpleTag extends SimpleTagSupport{
	public void doTag() throws JspException,IOException{
		getJspContext().getOut().write("hello world");
	}
}

 

package net.sx.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class AttributeTag extends SimpleTagSupport{
	private String name;
	private String age;
	
	
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getAge() {
		return age;
	}


	public void setAge(String age) {
		this.age = age;
	}


	public void doTag() throws JspException,IOException{
		getJspContext().getOut().write("hello world " + name  + ",age=" + age);
	}
}
 
package net.sx.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class BodyTag extends SimpleTagSupport{
	private String item;
	
	public String getItem() {
		return item;
	}

	public void setItem(String item) {
		this.item = item;
	}

	public void doTag() throws JspException,IOException{
		for(int i=0;i<5;i++){
			getJspContext().setAttribute(item, i);
			getJspBody().invoke(null);
		}
	}
}
 

 

<?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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <description>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>SimpleTagLibrary</short-name>
    <uri>http://taglib.sx.net</uri>
    <tag>
    <name>helloWorld</name>
	<tag-class>net.sx.taglib.SimpleTag</tag-class>
	<body-content>empty</body-content>
    </tag>
    
    <tag>
    <name>attributeT</name>
    <tag-class>net.sx.taglib.AttributeTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
    <name>name</name>
    <required>true</required>
    <fragment>true</fragment>
    </attribute>
    <attribute>
    <name>age</name>
    <required>true</required>
    <fragment>true</fragment>
    </attribute>
    </tag>
    
    <tag>
    <name>bodyT</name>
    <tag-class>net.sx.taglib.BodyTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
    <name>item</name>
    <required>true</required>
    <fragment>true</fragment>
    </attribute>
    </tag>
 </taglib>

 
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://taglib.sx.net"  prefix="mytag"%>
<html>
  <head></head>
   <body>
    <mytag:helloWorld/><br>
	<mytag:attributeT name="severus" age="11"/><br>
	<mytag:bodyT item="var">
	${pageScope.var}
	</mytag:bodyT>

  </body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值