JSP自定义简单标签
程序员文章站
2022-06-09 10:05:44
...
1.创建stls.tld文件,放在web项目的WEB-INF目录下
<?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://www.stls.com</uri>
<tag>
<name>demo1</name>
<tag-class>com.tls.web.simpletag.SimpleTagDemo1</tag-class>
<body-content>scriptless</body-content><!-- 标签体不能使用脚本表达式 -->
</tag>
<tag>
<name>demo2</name>
<tag-class>com.tls.web.simpletag.SimpleTagDemo2</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>demo3</name>
<tag-class>com.tls.web.simpletag.SimpleTagDemo3</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>demo4</name>
<tag-class>com.tls.web.simpletag.SimpleTagDemo4</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>demo5</name>
<tag-class>com.tls.web.simpletag.SimpleTagDemo5</tag-class>
<body-content>empty</body-content>
</tag>
<tag>
<name>demo6</name>
<tag-class>com.tls.web.simpletag.SimpleTagDemo6</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue><!-- 是否可以使用表达式赋值 -->
</attribute>
<attribute>
<name>date</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
2.创建简单标签执行类
package com.tls.web.simpletag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//控制标签体是否输出
public class SimpleTagDemo1 extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
JspFragment jspBody2 = this.getJspBody();
jspBody2.invoke(this.getJspContext().getOut());
}
}
package com.tls.web.simpletag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//控制标签体不输出
public class SimpleTagDemo2 extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
}
}
package com.tls.web.simpletag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//控制标签体循环输出
public class SimpleTagDemo3 extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
JspFragment jspBody2 = this.getJspBody();
for (int i = 0; i < 5; i++) {
jspBody2.invoke(null);//相当于jspBody2.invoke(this.getJspContext().getOut());
}
}
}
package com.tls.web.simpletag;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//修改标签体后输出
public class SimpleTagDemo4 extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
JspFragment jf = this.getJspBody();
StringWriter sw=new StringWriter();
jf.invoke(sw);
String content = sw.toString();
content = content.toUpperCase();
this.getJspContext().getOut().write(content);
}
}
package com.tls.web.simpletag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.SkipPageException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//标签体之后的不输出输出
public class SimpleTagDemo5 extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
throw new SkipPageException();
}
}
package com.tls.web.simpletag;
import java.io.IOException;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//标签体之后的不输出输出
public class SimpleTagDemo6 extends SimpleTagSupport {
private int count;
private Date date;
public void setDate(Date date) {
this.date = date;
}
public void setCount(int count) {
this.count = count;
}
@Override
public void doTag() throws JspException, IOException {
JspFragment jspBody2 = this.getJspBody();
this.getJspContext().getOut().write(date.toLocaleString()+"<br />");
for (int i = 0; i < count; i++) {
jspBody2.invoke(null);//相当于jspBody2.invoke(this.getJspContext().getOut());
}
}
}
3.使用标签
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.stls.com" prefix="stls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>简单标签</title>
</head>
<body>
标签体输出
<stls:demo1>
aaa
</stls:demo1>
<br />
标签体不输出
<stls:demo2>
aaa
</stls:demo2>
<br />
标签体循环输出
<stls:demo3>
aaa
</stls:demo3>
<br />
标签体修改后输出
<stls:demo4>
aaa
</stls:demo4>
<br />
必须要使用脚本表达式传日期,因为标签属性赋值只能转8中基本数据类型,不能转日期类型
<br />
<stls:demo6 count="9" date="<%=new Date() %>">
aaa
</stls:demo6>
<stls:demo5 />
aaaa
</body>
</html>