目录
- 目录
- 简介
- 使用标签控制页面逻辑的案例
- 开发防盗链标签
- 开发cif标签
- 开发cifcelse标签
- 开发cforEach标签
- 使用简单标签实现html转移标签
- 打包标签库
简介
让标签处理器类继承于SimpleTagSupport
类实现自定义标签功能。
以下案例的标签描述默认声明在 example.tld 中,如:
example.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>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>WM103-Example</short-name>
<uri>/example</uri>
</taglib>
简单标签中继承SimpleTagSupport
类的doTag
方法为空方法,默认不执行标签体。
使用标签控制页面逻辑的案例
开发防盗链标签
RerfererTag.java
package com.wm103.web.example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class RefererTag extends SimpleTagSupport {
private String site; // 允许访问的站点
private String page; // 盗链时的跳转页面
public void setSite(String site) {
this.site = site;
}
public void setPage(String page) {
this.page = page;
}
@Override
public void doTag() throws JspException, IOException {
PageContext pageContext = (PageContext) this.getJspContext();
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
String referer = request.getHeader("referer");
if(referer == null || !referer.startsWith(this.site)) {
String contextPath = request.getContextPath();
HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
if(this.page.startsWith(contextPath)) {
response.sendRedirect(this.page);
} else if(this.page.startsWith("/")) {
response.sendRedirect(contextPath + this.page);
} else {
response.sendRedirect(contextPath + "/" + this.page);
}
}
}
}
example.tld
<tag>
<name>referer</name>
<tag-class>com.wm103.web.example.RefererTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>site</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/example" prefix="e"%>
<e:referer site="http://localhost" page="/index.jsp"/>
<html>
<head>
<title>使用简单标签实现防盗链</title>
</head>
<body>
<p>我是防盗的内容!!!</p>
</body>
</html>
开发标签
IfTag.java
package com.wm103.web.example;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class IfTag extends SimpleTagSupport {
private boolean test;
public void setTest(boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
if(test) {
this.getJspBody().invoke(null);
}
}
}
example.tld
<tag>
<name>if</name>
<tag-class>com.wm103.web.example.IfTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/example" prefix="e"%>
<html>
<head>
<title>使用简单标签实现if标签</title>
</head>
<body>
<%
session.setAttribute("user", "DreamBoy");
%>
<e:if test="${user == null}">
<p>用户未登录!</p>
</e:if>
<e:if test="${user != null}">
<p>用户已登录!</p>
</e:if>
</body>
</html>
开发标签
3.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/example" prefix="e"%>
<html>
<head>
<title>使用简单标签实现if else标签</title>
</head>
<body>
<%
session.setAttribute("user", "DreamBoy");
%>
<e:choose>
<e:when test="${user == null}">
<p>用户未登录!</p>
</e:when>
<e:otherwise>
<p>用户已登录!</p>
</e:otherwise>
</e:choose>
</body>
</html>
ChooseTag.java
package com.wm103.web.example;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class ChooseTag extends SimpleTagSupport {
private boolean isDo;
public boolean isDo() {
return isDo;
}
public void setDo(boolean aDo) {
isDo = aDo;
}
@Override
public void doTag() throws JspException, IOException {
this.getJspBody().invoke(null);
}
}
WhenTag.java
package com.wm103.web.example;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class WhenTag extends SimpleTagSupport {
private boolean test;
public void setTest(boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
ChooseTag parent = (ChooseTag) this.getParent();
if(test && !parent.isDo()) {
this.getJspBody().invoke(null);
parent.setDo(true);
}
}
}
OtherwiseTag.java
package com.wm103.web.example;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class OtherwiseTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
ChooseTag parent = (ChooseTag) this.getParent();
if(!parent.isDo()) {
this.getJspBody().invoke(null);
parent.setDo(true);
}
}
}
example.tld
<tag>
<name>choose</name>
<tag-class>com.wm103.web.example.ChooseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>when</name>
<tag-class>com.wm103.web.example.WhenTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>otherwise</name>
<tag-class>com.wm103.web.example.OtherwiseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
开发标签
ForEachTag.java
package com.wm103.web.example;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
public class ForEachTag extends SimpleTagSupport {
Object items;
String var;
public void setVar(String var) {
this.var = var;
}
/*public void setItems(Object items) {
this.items = items;
}*/
/*@Override
public void doTag() throws JspException, IOException {
List list = (List) items;
Iterator it = list.iterator();
while (it.hasNext()) {
Object value = it.next();
this.getJspContext().setAttribute(this.var, value);
this.getJspBody().invoke(null);
}
}*/
private Collection collection;
public void setItems(Object items) {
this.items = items;
this.collection = new ArrayList<>();
if(items instanceof Collection) {
this.collection = (Collection) items;
} else if(items instanceof Map) {
Map map = (Map) items;
this.collection = map.entrySet();
} else if(items.getClass().isArray()) {
int length = Array.getLength(items);
for(int i = 0; i < length; i++) {
this.collection.add(Array.get(items, i));
}
}
}
@Override
public void doTag() throws JspException, IOException {
Iterator it = this.collection.iterator();
while (it.hasNext()) {
Object value = it.next();
this.getJspContext().setAttribute(this.var, value);
this.getJspBody().invoke(null);
}
}
}
example.tld
<tag>
<name>forEach</name>
<tag-class>com.wm103.web.example.ForEachTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
4.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<%@ taglib uri="/example" prefix="e"%>
<html>
<head>
<title>使用简单标签实现forEach标签</title>
</head>
<body>
<%
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
request.setAttribute("list", list);
%>
<e:forEach var="value" items="${list}">
<p>${value}</p>
</e:forEach>
<%
Map map = new HashMap();
map.put("aaa", "a1");
map.put("bbb", "b1");
map.put("ccc", "c1");
map.put("ddd", "d1");
request.setAttribute("map", map);
%>
<e:forEach var="entry" items="${map}">
<p>${entry.key} -- ${entry.value}</p>
</e:forEach>
<%
int[] array = {1, 2, 3, 4};
request.setAttribute("array", array);
%>
<e:forEach var="value" items="${array}">
<p>${value}</p>
</e:forEach>
</body>
</html>
使用简单标签实现html转移标签
HtmlFilter.java
package com.wm103.web.example;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.StringWriter;
public class HtmlFilterTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
StringWriter writer = new StringWriter();
JspFragment jf = this.getJspBody();
jf.invoke(writer);
String content = writer.getBuffer().toString();
content = filter(content); // 进行转义
this.getJspContext().getOut().write(content);
}
public String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuilder result = new StringBuilder(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
example.tld
<tag>
<name>htmlFilter</name>
<tag-class>com.wm103.web.example.HtmlFilterTag</tag-class>
<body-content>scriptless</body-content>
</tag>
5.jsp
<e:htmlFilter>
<a href="">点击链接</a>
</e:htmlFilter>
打包标签库
- 创建Java工程,并把标签处理类复制到工程中;
- 在Java工程中创建
META-INF
目录,并将标签库描述文件放置在该目录下; - 将Java工程导出为jar包;
- 在Java Web工程中引入该jar包即可使用jar包中定义的标签。