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>