文章目录
编写自定义标签
版权标签
package com.eshore;
import jakarta.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
public class CopyRightTag extends TagSupport{
@Override
public int doStartTag(){
String copyRight = "版权所有 ©2018";
try {
pageContext.getOut().print(copyRight);
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
public int doEndTag(){
return EVAL_PAGE;
}
}
<?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>linl</short-name>
<uri>/copyr-tags</uri>
<!--上述文件中只有一个taglib标签,而taglib标签下可以有多个tag标签,每个tag标签代表一个自定义标签-->
<tag>
<!-- 自定义标签的功能描述 -->
<description>自定义版权</description>
<name>copyright</name>
<!-- 自定义标签的实现类路径 -->
<tag-class>com.eshore.CopyRightTag</tag-class>
<!-- 正文内容类型 没有正文内容用empty表示 -->
<body-content>empty</body-content>
</tag>
</taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="linl" uri="/copyr-tags" %>
<!DOCTYPE HTML >
<html>
<head>
<title>自定义版本标签示例</title>
</head>
<body>
<p>这里是正文的内容</p>
<linl:copyright/>
</body>
</html>
tld标签库描述文件
tld文件中常用的元素有taglib,tag,attribute,variable。
< t a g l i b > <taglib> <taglib>元素用来设置整个标签库信息的; < t a g > <tag> <tag>元素用来定义标签里面的具体内容; < a t t r i b u t e > <attribute> <attribute>元素用来定义 < t a g > <tag> <tag>中的属性; < v a r i a b l e > <variable> <variable>元素用来定义 < t a g > <tag> <tag>中的变量属性。
TagSupport类
自定义标签库的实现类大多继承自TagSupport类来实现自身的方法,其有四个重要接口:
带参数的自定义标签
package com.eshore;
import jakarta.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CopyRightTagV extends BodyTagSupport{
private String user;
private String startY;
@Override
public int doStartTag(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String endY = sdf.format(new Date());
String copyRight = user+" 版权所有 "+startY+"-"+endY;
try {
pageContext.getOut().print(copyRight);
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
public int doEndTag(){
return EVAL_PAGE;
}
public void setUser(String user) {
this.user = user;
}
public void setStartY(String startY) {
this.startY = startY;
}
}
<?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>linl</short-name>
<uri>/copyrightv-tags</uri>
<tag>
<!-- 自定义标签的功能描述 -->
<description>自定义版权</description>
<name>copyrightV</name>
<!-- 自定义标签的实现类路径 -->
<tag-class>com.eshore.CopyRightTagV</tag-class>
<!-- 正文内容类型 没有正文内容用empty表示 -->
<body-content>empty</body-content>
<!-- 添加attribute属性 -->
<attribute>
<description>版权拥有者</description>
<name>user</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>开始时间</description>
<name>startY</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="linl" uri="/copyrightv-tags" %>
<!DOCTYPE HTML >
<html>
<head>
<title>自定义版本标签示例</title>
</head>
<body>
<p>这里是正文的内容</p>
<linl:copyrightV startY="2013" user="林龙"/>
</body>
</html>
带标签体的自定义标签
package com.eshore;
import jakarta.servlet.jsp.JspWriter;
import jakarta.servlet.jsp.tagext.BodyContent;
import jakarta.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CopyRightBodyContentTag extends BodyTagSupport {
private String user;
private String startY;
@Override
public int doStartTag(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String endY = sdf.format(new Date());
String copyRight = user+" 版权所有 "+startY+"-"+endY;
try {
pageContext.getOut().print(copyRight);
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
public int doAfterBody(){
//取得标签体
BodyContent bc = getBodyContent();
JspWriter out = getPreviousOut();
try{
//将标签中的内容写入到JSP页面中
out.write(bc.getString());
}catch(IOException e){
e.printStackTrace();
}
return SKIP_BODY;
}
public int doEndTag(){
return EVAL_PAGE;
}
public void setUser(String user) {
this.user = user;
}
public void setStartY(String startY) {
this.startY = startY;
}
}
<?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>linl</short-name>
<uri>/copyrightBodycontent-tags</uri>
<tag>
<!-- 自定义标签的功能描述 -->
<description>自定义版权</description>
<name>copyright</name>
<!-- 自定义标签的实现类路径 -->
<tag-class>com.eshore.CopyRightBodyContentTag</tag-class>
<!-- 正文内容类型 允许有JSP代码-->
<body-content>JSP</body-content>
<!-- 添加attribute属性 -->
<attribute>
<description>版权拥有者</description>
<name>user</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>开始时间</description>
<name>startY</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
<tag>
<!-- 自定义标签的功能描述 -->
<description>自定义版权</description>
<name>loop</name>
<!-- 自定义标签的实现类路径 -->
<tag-class>com.eshore.CopyRightBodyContentTag</tag-class>
<!-- 正文内容类型 允许有JSP代码-->
<body-content>JSP</body-content>
<!-- 添加attribute属性 -->
<attribute>
<description>循环次数</description>
<name>time</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Integer</type>
</attribute>
</tag>
</taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="linl" uri="/copyrightBodycontent-tags" %>
<!DOCTYPE HTML >
<html>
<head>
<title>自定义版本标签示例</title>
</head>
<body>
<p>这里是正文的内容</p>
<linl:copyright startY="2013" user="林龙">
<a href="http://www.sina.com">新浪网</a>
</linl:copyright>
</body>
</html>
多次执行的循环标签
package com.eshore;
import jakarta.servlet.jsp.JspWriter;
import jakarta.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
public class CopyRightTagLoop extends BodyTagSupport{
private static final long serialVersionUID = 1L;
private int time;
@Override
public int doStartTag(){
return EVAL_BODY_INCLUDE;
}
public int doAfterBody(){
if(time>1){
time--;
return EVAL_BODY_AGAIN;
}else{
return SKIP_BODY;
}
}
public int doEndTag(){
JspWriter out = pageContext.getOut();
try {
out.print("");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EVAL_PAGE;
}
public void setTime(int time) {
this.time = time;
}
}
<?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>linl</short-name>
<uri>/copyrightBodycontent-tags</uri>
<tag>
<!-- 自定义标签的功能描述 -->
<description>自定义版权</description>
<name>copyright</name>
<!-- 自定义标签的实现类路径 -->
<tag-class>com.eshore.CopyRightBodyContentTag</tag-class>
<!-- 正文内容类型 允许有JSP代码-->
<body-content>JSP</body-content>
<!-- 添加attribute属性 -->
<attribute>
<description>版权拥有者</description>
<name>user</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>开始时间</description>
<name>startY</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
<tag>
<!-- 自定义标签的功能描述 -->
<description>自定义版权</description>
<name>loop</name>
<!-- 自定义标签的实现类路径 -->
<tag-class>com.eshore.CopyRightTagLoop</tag-class>
<!-- 正文内容类型 允许有JSP代码-->
<body-content>JSP</body-content>
<!-- 添加attribute属性 -->
<attribute>
<description>循环次数</description>
<name>time</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Integer</type>
</attribute>
</tag>
</taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="linl" uri="/copyrightBodycontent-tags" %>
<!DOCTYPE HTML >
<html>
<head>
<title>自定义版本标签示例</title>
</head>
<body>
<p>这里是正文的内容</p>
<linl:loop time="5">
<a href="http://www.sina.com">新浪网</a><br/>
</linl:loop>
</body>
</html>
带动态属性的自定义标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="linl" uri="/copyrightBodycontent-tags" %>
<!DOCTYPE HTML >
<html>
<head>
<title>自定义版本标签示例</title>
</head>
<%
int num = (int)(Math.random()*6)+1;
%>
<body>
<p>这里是正文的内容</p>
<linl:loop time="<%=num%>">
<a href="http://www.sina.com">新浪网</a><br/>
</linl:loop>
</body>
</html>
嵌套的自定义标签
package com.eshore;
public class UserInfo {
private String userName;//用户名
private int age;//年龄
private String email;//用户邮箱
public UserInfo(String userName, int age, String email) {
super();
this.userName = userName;
this.age = age;
this.email = email;
}
public UserInfo() {
super();
// TODO Auto-generated constructor stub
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package com.eshore;
import jakarta.servlet.jsp.JspException;
import jakarta.servlet.jsp.JspWriter;
import jakarta.servlet.jsp.tagext.TagSupport;
public class UserInfoTag extends TagSupport{
private static final long serialVersionUID = 1L;
private UserInfo user;
@Override
public int doStartTag() throws JspException {
try {
JspWriter out = this.pageContext.getOut();
if(user == null) {
out.println("No UserInfo Found...");
return SKIP_BODY;
}
String content = "<td>"+user.getUserName()+"</td>";
content+="<td>"+user.getAge()+"</td>";
content+="<td>"+user.getEmail()+"</td>";
out.println("<tr>"+content+"</tr>");
} catch(Exception e) {
throw new JspException(e.getMessage());
}
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
@Override
public void release() {
//释放资源
super.release();
this.user = null;
}
public UserInfo getUser() {
return user;
}
public void setUser(UserInfo user) {
this.user = user;
}
}
package com.eshore;
import jakarta.servlet.jsp.JspException;
import jakarta.servlet.jsp.tagext.TagSupport;
import java.util.Collection;
import java.util.Iterator;
public class TableTag extends TagSupport {
private static final long serialVersionUID = 1L;
private Collection items;
private Iterator it;
private String var;
@Override
public int doStartTag() throws JspException {
if(items == null || items.size() == 0) return SKIP_BODY;
it = items.iterator();
if(it.hasNext()) {
pageContext.setAttribute(var, it.next());
}
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
if(it.hasNext()) {
pageContext.setAttribute(var, it.next());
return EVAL_BODY_AGAIN;
}
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
public void setItems(Collection items) {
this.items = items;
}
public void setVar(String var) {
this.var = var;
}
}
<?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>linl</short-name>
<uri>/table-tags</uri>
<!-- 显示表格标签内容 -->
<tag>
<name>showUserInfo</name>
<tag-class>com.eshore.UserInfoTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>user</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!-- 表格标签遍历 -->
<tag>
<name>table</name>
<tag-class>com.eshore.TableTag</tag-class>
<body-content>JSP</body-content>
<!-- 属性 -->
<attribute>
<name>items</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
<%@ page language="java" import="java.util.*,com.eshore.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="linl" uri="/table-tags" %>
<!DOCTYPE HTML >
<html>
<head>
<title>自定义表格标签示例</title>
</head>
<%
//模拟从数据库中取出数据
List<UserInfo> users = new ArrayList<UserInfo>();
users.add(new UserInfo("张三", 20, "[email protected]"));
users.add(new UserInfo("李四", 26, "[email protected]"));
users.add(new UserInfo("王五", 33, "[email protected]"));
pageContext.setAttribute("users", users);
%>
<body>
<div style="text-align: center;">
用户信息<br/>
</div>
<table width='400px' border='1' align='center'>
<tr>
<td width='20%'>用户名</td>
<td width='20%'>年龄</td>
<td>邮箱</td>
</tr>
<!-- 使用标签输出用户信息 -->
<linl:table var="item" items="${users}">
<linl:showUserInfo user="${item}" />
</linl:table>
</table>
</body>
</html>
JSP2.X标签
package com.eshore;
import jakarta.servlet.jsp.JspException;
import jakarta.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleTagCopyRight extends SimpleTagSupport{
private String user;
private String startY;
@Override
public void doTag() throws JspException, IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String endY = sdf.format(new Date());
String copyRight = user+" ��Ȩ���� "+startY+"-"+endY;
getJspContext().getOut().write(copyRight);
}
public void setUser(String user) {
this.user = user;
}
public void setStartY(String startY) {
this.startY = startY;
}
}
<?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>linl</short-name>
<uri>/simpleTagCopyright-tags</uri>
<tag>
<!-- 自定义标签的功能描述 -->
<description>SimpleTag版权</description>
<name>simpleTagcopyright</name>
<!-- 自定义标签的实现类路径 -->
<tag-class>com.eshore.SimpleTagCopyRight</tag-class>
<!-- 正文内容类型 没有正文内容用empty表示 -->
<body-content>empty</body-content>
<!-- 添加attribute属性 -->
<attribute>
<description>版权拥有者</description>
<name>user</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<description>开始时间</description>
<name>startY</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="linl" uri="/simpleTagCopyright-tags" %>
<!DOCTYPE HTML >
<html>
<head>
<title>自定义版本标签示例</title>
</head>
<body>
<p>这里是正文的内容</p>
<linl:simpleTagcopyright startY="2013" user="林龙"/>
</body>
</html>