helloworld2.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="please" uri="/WEB-INF/lang.tld"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<please:helloWorld2 name="jspservlet吊炸天"/>
</body>
</html>
Helloworld2.java
package com.lang.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class HelloWorldTag2 extends TagSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = this.pageContext.getOut();
try {
out.println(name+"自定义标签");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TagSupport.SKIP_BODY; //直接结束标签
}
}
配置文件lang.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>pleaseTag</short-name>
<tag>
<name>helloWorld2</name>
<tag-class>
com.lang.tag.HelloWorldTag2
</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
运行结果:
自定义标签属性有: