z注意每个uri地址要保持统一
1.创建MytagPrinta.java文件
package cn.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
public class MytagPrinta extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
protected String value = null;
protected String type = "0";
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
try {
if(type.equals("0"))
{
pageContext.getOut().println("hello:" + value);
}else{
pageContext.getOut().print("hell:"+value+"<br>");
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new JspTagException( e.getMessage() );
}
return SKIP_PAGE;
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return EVAL_PAGE;
}
}
2.配置web-info下的web.xml文件
<jsp-config>
<taglib>
<taglib-uri>http://www.tag.com/mytag</taglib-uri>
<taglib-location>/WEB-INF/mytlds/mytag.tld</taglib-location>
</taglib>
</jsp-config>
3.web-info下的mytlds文件夹下创建 mytag.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
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>mytag 带type和value属性的打印</description>
<display-name>mytag 标签库</display-name>
<tlib-version>1.0</tlib-version>
<short-name>mytag</short-name>
<uri>http://www.tag.com/mytag</uri>
<tag>
<description>打印value和type</description>
<name>printa</name>
<tag-class>cn.tag.MytagPrinta</tag-class>
<body-content>empty</body-content>
<attribute>
<name>type</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
4.新建tag.jsp文件。调用
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="mytag" uri="http://www.tag.com/mytag" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>带属性标签实例</title>
</head>
<body>
<h3>调用带属性的print标签</h3>
打印后换行: <mytag:printa value="print" type="1"/>
打印后不换行:<mytag:printa value="printa" type="0"/>
</body>
</html>