javaweb学习总结(二十七)——jsp简单标签开发案例和打包

一、开发标签库

1.1、开发防盗链标签

  1、编写标签处理器类:RefererTag.java

复制代码
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpServletResponse;
 6 import javax.servlet.jsp.JspException;
 7 import javax.servlet.jsp.PageContext;
 8 import javax.servlet.jsp.SkipPageException;
 9 import javax.servlet.jsp.tagext.SimpleTagSupport;
10 
11 /**
12  * @author gacl
13  * 防盗链标签RefererTag
14  */
15 public class RefererTag extends SimpleTagSupport {
16 
17     /**
18      * 网站域名
19      */
20     private String site;   //页面上传来的值:http://192.168.188.53:7001
21     
22     /**
23      * 要跳转的页面
24      */
25     private String page;   //页面上传来的值:/index.jsp 26     
27     @Override
28     public void doTag() throws JspException, IOException {
29         //获取jsp页面的PageContext对象
30         PageContext pageContext = (PageContext) this.getJspContext();
31         //通过PageContext对象来获取HttpServletRequest对象
32         HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
33         //获取请求的来路(Referer)
34         String referer = request.getHeader("referer"); //http://192.168.188.53.:7001/xmlDemo/index.jsp 不知道为什么53后面有一个.点
35         //如果来路是null或者来路不是来自我们自己的site,那么就将请求重定向到page页面
36         if (referer == null || !referer.startsWith(site)) { //原文中的代码:没有效果,一直页面不变          
         if (referer == null || referer.startsWith(site)) { //修改后的代码,自己测试有效果
 

37             //获取HttpServletResponse对象
38             HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
39             String webRoot = request.getContextPath();
40             if (page.startsWith(webRoot)) {
41                 //重定向到page页面
42                 response.sendRedirect(page);
43             } else {
44                 //重定向到page页面
45                 response.sendRedirect(webRoot+page);
46             }
47             //重定向后,控制保护的页面不要执行
48             throw new SkipPageException();
49         }
50     }
51 
52     public void setSite(String site) {
53         this.site = site;
54     }
55 
56     public void setPage(String page) {
57         this.page = page;
58     }
59 }
复制代码

  2、在WEB-INF目录下tld文件中添加对该标签的描述,如下:

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
 6     
 7     <description>孤傲苍狼开发的简单标签库</description>
 8     <tlib-version>1.0</tlib-version>
 9     <short-name>TagLib</short-name>
10     <uri>/gaclTagLib</uri>
11     
12     <tag>
13         <!-- 标签名 -->
14         <name>referer</name>
15         <!-- 标签处理器类 -->
16         <tag-class>me.gacl.web.simpletag.RefererTag</tag-class>
17         <!-- 标签体允许的内容 -->
18         <body-content>empty</body-content>
19         <!-- 标签的属性描述 -->
20         <attribute>
21             <description>描述标签的site属性</description>
22             <!-- 标签的site属性 -->
23             <name>site</name>
24             <required>true</required>
25             <!-- rtexprvalue用来指示标签的属性值是否可以是一个表达式, 一般设置为true,true就表示允许标签的属性值可以是一个表达式 -->
26             <rtexprvalue>true</rtexprvalue>
27         </attribute>
28         <attribute>
29             <description>描述标签的page属性</description>
30             <!-- 标签的page属性 -->
31             <name>page</name>
32             <required>true</required>
33             <!-- rtexprvalue用来指示标签的属性值是否可以是一个表达式, 一般设置为true,true就表示允许标签的属性值可以是一个表达式 -->
34             <rtexprvalue>true</rtexprvalue>
35         </attribute>
36     </tag>
37     
38 </taglib>
复制代码

  3、测试:在jsp页面中导入标签库并使用防盗链标签

复制代码
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <%--在jsp页面中导入自定义标签库 --%>
 3 <%@taglib uri="/gaclTagLib" prefix="gacl" %>
 4 <%--在jsp页面中也可以使用这种方式导入标签库,直接把uri设置成标签库的tld文件所在目录
 5 <%@taglib uri="/WEB-INF/TagLib.tld" prefix="gacl"%>
 6 --%>
 7 <%--在Jsp页面中使用防盗链标签 
 8 当用户尝试直接通过URL地址(http://localhost:8080/JavaWeb_JspTag_study_20140816/simpletag/refererTagTest.jsp)访问这个页面时,
 9 防盗链标签的标签处理器内部就会进行处理,将请求重定向到/index.jsp
10 --%>
11 <gacl:referer site="http://localhost:8080" page="/index.jsp"/>
12 <!DOCTYPE HTML>
13 <html>
14   <head>
15     <title>防盗链标签测试</title>
16   </head>
17 
18   <body>
19      网站内部资料
20   </body>
21 </html>
复制代码

  运行效果如下:

  

1.2、开发<c:if>标签

  1、编写标签处理器类:IFTag.java

复制代码
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * 开发if标签
10  */
11 public class IFTag extends SimpleTagSupport {
12 
13     /**
14      * if标签的test属性
15      */
16     private boolean test;
17     
18     @Override
19     public void doTag() throws JspException, IOException {
20         if (test) {
21             this.getJspBody().invoke(null);
22         }
23     }
24 
25     public void setTest(boolean test) {
26         this.test = test;
27     }
28 }
复制代码

  2、在WEB-INF目录下tld文件中添加对该标签的描述,如下:

复制代码
 1 <tag>
 2     <description>if标签</description>
 3     <name>if</name>
 4     <tag-class>me.gacl.web.simpletag.IFTag</tag-class>
 5     <body-content>scriptless</body-content>
 6     <attribute>
 7         <description>if标签的test属性</description>
 8         <name>test</name>
 9         <rtexprvalue>true</rtexprvalue>
10         <required>true</required>
11     </attribute>
12 </tag>
复制代码

  3、测试:在jsp页面中导入标签库并使用if标签

复制代码
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <%--在jsp页面中导入自定义标签库 --%>
 3 <%@taglib uri="/gaclTagLib" prefix="c" %>
 4 <%--在jsp页面中也可以使用这种方式导入标签库,直接把uri设置成标签库的tld文件所在目录
 5 <%@taglib uri="/WEB-INF/TagLib.tld" prefix="c"%>
 6 --%>
 7 <!DOCTYPE HTML>
 8 <html>
 9   <head>
10     <title>if链标签测试</title>
11   </head>
12 
13   <body>
14   <%--if标签的test属性值为true ,标签体的内容会输出--%>
15       <c:if test="true">
16          <h3>网站内部资料</h3>
17     </c:if>
18     <%--if标签的test属性值为false ,标签体的内容不会输出--%>
19     <c:if test="false">
20         这里的内部不输出
21     </c:if>
22   </body>
23 </html>
复制代码

  运行效果如下:

  

1.3、开发<c:when><c:otherwise>标签

  这个标签的开发稍微有一点难度,因为这里面涉及到两个标签处理器类共享同一个变量的问题,如下:

1 <c:when test="${user != null}">
2     用户不为空
3 </c:when>
4 <c:otherwise>
5     用户为空
6 </c:otherwise>

  <c:when>标签和<c:otherwise>标签对应着两个不同的标签处理器类,我们希望做到的效果是,如果<c:when>标签执行了,那么就<c:otherwise>标签就不要再执行,那么这里面就涉及到一个问题:<c:when>标签执行的时候该如何通知<c:otherwise>标签不要执行了呢?这个问题就涉及到了两个标签处理器类如何做到相互通讯的问题,如果<c:when>标签执行了,就要通过某种方式告诉<c:otherwise>标签不要执行,那么该如何做到这样的效果呢?让<c:when>标签处理器类和<c:otherwise>标签处理器类共享同一个变量就可以做到了,那么又该怎么做才能够让两个标签处理器类共享同一个变量呢,标准的做法是这样的:让两个标签拥有同一个父标签。

 1、开发父标签:ChooseTag.java

复制代码
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * when标签和otherwise标签的父标签
10  */
11 public class ChooseTag extends SimpleTagSupport {
12 
13     /**
14      * 定义一个boolean类型的属性,该属性用于标识该标签下的某一个子标签是否已经执行过了,
15      * 如果该标签下的某一个子标签已经执行过了,就将该属性设置为true
16      */
17     private boolean isExecute;
18     
19     @Override
20     public void doTag() throws JspException, IOException {
21         //输出标签体中的内容
22         this.getJspBody().invoke(null);
23     }
24 
25     public boolean isExecute() {
26         return isExecute;
27     }
28 
29     public void setExecute(boolean isExecute) {
30         this.isExecute = isExecute;
31     }
32 }
复制代码

  2、开发when标签和otherwise标签

  WhenTag.java

复制代码
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * when标签
10  */
11 public class WhenTag extends SimpleTagSupport {
12 
13     /**
14      * test属性,该属性值为true时,输出标签体中的内容
15      */
16     private boolean test;
17复制代码
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * when标签
10  */
11 public class WhenTag extends SimpleTagSupport {
12 
13     /**
14      * test属性,该属性值为true时,输出标签体中的内容
15      */
16     private boolean test;
17     
18     @Override
19     public void doTag() throws JspException, IOException {
20         //获取标签的父标签
21         ChooseTag parentTag = (ChooseTag) this.getParent();
22         if (test == true && parentTag.isExecute() == false) {
23             //输出标签体中的内容
24             this.getJspBody().invoke(null);
25             //将父标签的isExecute属性设置为true,告诉父标签,我(when标签)已经执行过了
26             parentTag.setExecute(true);
27         }
28     }
29 
30     public void setTest(boolean test) {
31         this.test = test;
32     }
33 }
复制代码

  OtherWiseTag.java

复制代码
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * otherwise标签
10  */
11 public class OtherWiseTag extends 6 
 7 /**
 8  * @author gacl
 9  * when标签
10  */
11 public class WhenTag extends SimpleTagSupport {
12 
13     /**
14      * test属性,该属性值为true时,输出标签体中的内容
15      */
16     private boolean test;
17     
18     @Override
19     public void doTag() throws JspException, IOException {
20         //获取标签的父标签
21         ChooseTag parentTag = (ChooseTag) this.getParent();
22         if (test == true && parentTag.isExecute() == false) {
23             //输出标签体中的内容
24             this.getJspBody().invoke(null);
25             //将父标签的isExecute属性设置为true,告诉父标签,我(when标签)已经执行过了
26             parentTag.setExecute(true);
27         }
28     }
29 
30     public void setTest(boolean test) {
31         this.test = test;
32     }
33 }
复制代码

  OtherWiseTag.java

复制代码
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * otherwise标签
10  */
11 public class OtherWiseTag extends SimpleTagSupport {
12 
13     @Override
14     public true && parentTag.isExecute() == false) {
23             //输出标签体中的内容
24             this.getJspBody().invoke(null);
25             
相关文章
相关标签/搜索