本文為大家介紹了JSP自定義標簽的案例,供大家參考,具體內容如下
案例一:實現一個基本防盜鏈標簽
1. 標簽處理類
public class MyReferer extends BodyTagSupport {
private String site;
private String back;
public String getSite() {
return site;
}
public void setSite(String site) {
this.site = site;
}
public String getBack() {
return back;
}
public void setBack(String back) {
this.back = back;
}
public int doEndTag() throws JspException {
// 獲取JSP上下文環境對象
PageContext pageContext = this.pageContext;
// 獲取到request對象
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
// 判斷
String header = request.getHeader("referer");
if(header != null && header.startsWith(getSite())){
// 執行後續的頁面
return Tag.EVAL_PAGE;
}else{
// 頁面的重定向
HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
try {
response.sendRedirect(getBack());
} catch (IOException e) {
e.printStackTrace();
}
// 不執行
return Tag.SKIP_PAGE;
}
}
}
2. 描述文件
<?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">
<!-- 2. 編寫標簽庫描述文件 -->
<tlib-version>1.0</tlib-version>
<short-name>jnb</short-name>
<tag>
<name>referer</name>
<tag-class>cn.itcast.custom.MyReferer</tag-class>
<body-content>empty</body-content>
<attribute>
<name>site</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>back</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3. 引入和使用
<%@taglib uri="/WEB-INF/referer.tld" prefix="my"%> <my:referer site=http://localhost:8080/day11/list.jsp back="/day11/list.jsp"/>
JSP2.0自定義標簽
---| SimpleTag 接口
定義了標簽處理類的生命周期方法。doTag()
-----| SimpleTagSupport 類
全部實現了SimpleTag接口的方法,因此後面我們只需要繼承並重寫該類即可。
案例二:實現自己的if….else標簽
目標:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:choose>
<c:when test="<%= 12>1 %>">
大於
</c:when>
<c:otherwise>
小於
</c:otherwise>
</c:choose>
分析:
1. ChooseTag.java,必須定義一個標記字段屬性
public class ChooseTag extends SimpleTagSupport {
private boolean tag = true;
public boolean isTag() {
return tag;
}
public void setTag(boolean tag) {
this.tag = tag;
}
// 遇到標簽自動執行
public void doTag() throws JspException, IOException {
// 獲取標簽體對象
JspFragment body = this.getJspBody();
// 執行標簽體
body.invoke(null);
super.doTag();
}
}
2. WhenTag.java
public class WhenTag extends SimpleTagSupport {
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
// 遇到標簽自動執行
public void doTag() throws JspException, IOException {
// 獲取父元素
ChooseTag choose = (ChooseTag)this.getParent();
// 獲取父元素的標記變量值
boolean parent = choose.isTag();
// 判斷
if( parent && this.isTest() ){
// 執行標簽體
JspFragment body = this.getJspBody();
body.invoke(null);
}
super.doTag();
}
}
3. Otherwise.java
public class OtherwiseTag extends SimpleTagSupport {
// 遇到標簽自動執行
public void doTag() throws JspException, IOException {
// 獲取父元素
ChooseTag choose = (ChooseTag)this.getParent();
// 獲取父元素的標記變量值
boolean parent = choose.isTag();
// 判斷
if(parent){
// 執行標簽體
JspFragment body = this.getJspBody();
body.invoke(null);
}
super.doTag();
}
}
4. 描述文件
<?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">
<!-- 2. 編寫標簽庫描述文件 -->
<tlib-version>1.0</tlib-version>
<short-name>jnb</short-name>
<tag>
<name>choose</name>
<tag-class>cn.itcast.tags.ChooseTag</tag-class>
<body-content>scriptless</body-content> JSP2.0方式
</tag>
<tag>
<name>when</name>
<tag-class>cn.itcast.tags.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>cn.itcast.tags.OtherwiseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
5. 引入和使用
<%@taglib uri="/WEB-INF/ifelse.tld" prefix="jnb"%>
<jnb:choose>
<jnb:when test="<%= 1>2 %>">
小於
</jnb:when>
<jnb:otherwise>
大於
</jnb:otherwise>
</jnb:choose>
打包自定義標簽庫
1. 建立一個taglibs文件夾
2. 將所有的標簽處理類對應的class文件連同包拷貝到1中的目錄中
3. 在1中的文件夾中建立一個META-INF文件夾
4. 將tld文件拷貝到META-INF目錄
5. 編輯tld文件引入uri元素:<uri>http://www.jnb.com</uri> à提供引入的url路徑
6. 使用jar命令進行打包:D:\mytaglibs>jar cvf jnb.jar *
總結
主要掌握如何使用JSP2.0進行自定義標簽的開發和打包。
1. 建立一個taglibs文件夾
2. 將所有的標簽處理類對應的class文件連同包拷貝到1中的目錄中
3. 在1中的文件夾中建立一個META-INF文件夾
4. 將tld文件拷貝到META-INF目錄
5. 編輯tld文件引入uri元素:<uri>http://www.jnb.com</uri> à提供引入的url路徑
6. 使用jar命令進行打包:D:\mytaglibs>jar cvf jnb.jar *
總結
主要掌握如何使用JSP2.0進行自定義標簽的開發和打包。