三个标签案例:帮你深入学习JSP自定义标签

  1. /* 
  2. 案例一:开发一个if标签 
  3.  
  4. 日期:20130930 
  5.  
  6. 作者:烟大阳仔 
  7.  
  8. */  
  9. 1.编写一个实现tag接口的JAVA类  
  10. public class IFTagLib extends SimpleTagSupport   
  11. {  
  12.     private boolean test;  
  13.   
  14.     public void setTest(boolean test) {  
  15.         this.test = test;  
  16.     }  
  17.   
  18.     @Override  
  19.     public void doTag() throws JspException, IOException {  
  20.         if(test)  
  21.         {  
  22.             this.getJspBody().invoke(null);  
  23.         }  
  24.     }  
  25.       
  26. }  
  27.       
  28. 2.在tld文件中对标签处理器进行描述(tld文件的位置WEB-INF里面)  
  29. <?xml version="1.0" encoding="UTF-8" ?>  
  30. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
  31.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  32.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"  
  33.     version="2.0">  
  34.     <description>A tag library exercising SimpleTag handlers.</description>  
  35.     <tlib-version>1.0</tlib-version>  
  36.     <short-name>IFTagLib</short-name>  
  37.     <uri>/IFTagLib</uri>  
  38.     <tag>  
  39.         <name>if</name>  
  40.         <tag-class>cn.com.web.ifTag.IFTagLib</tag-class>  
  41.         <body-content>scriptless</body-content>  
  42.         <attribute>  
  43.             <name>test</name>  
  44.             <required>true</required>  
  45.             <rtexprvalue>true</rtexprvalue>  
  46.         </attribute>  
  47.     </tag>  
  48. </taglib>  
  49. 3.在jsp页面中使用标签  
  50. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  51. <%@taglib uri="/IFTagLib" prefix="IFTagLib" %>  
  52. <%  
  53. String path = request.getContextPath();  
  54. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  55. %>  
  56.   
  57. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  58. <html>  
  59.   <head>  
  60.     <base href="<%=basePath%>">  
  61.       
  62.     <title>My JSP 'IfDemo.jsp' starting page</title>  
  63.       
  64.     <meta http-equiv="pragma" content="no-cache">  
  65.     <meta http-equiv="cache-control" content="no-cache">  
  66.     <meta http-equiv="expires" content="0">      
  67.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  68.     <meta http-equiv="description" content="This is my page">  
  69.     <!--  
  70.     <link rel="stylesheet" type="text/css" href="styles.css">  
  71.     -->  
  72.   
  73.   </head>  
  74.     
  75.   <body>  
  76.     <%  
  77.         session.setAttribute("user""as");  
  78.     %>  
  79.     <IFTagLib:if test="${user==null}">  
  80.         Hello world!  
  81.     </IFTagLib:if>  
  82.     <IFTagLib:if test="${user!=null}">  
  83.         一点不好啊!  
  84.     </IFTagLib:if>  
  85.       
  86.   </body>  
  87. </html>  
  88.   
  89.   
  90. ----------------------------------------------------------------------------------------------------------  
  91.   
  92. /* 
  93. 案例二:开发一个嵌套的标签 
  94.     <IFElse:choose> 
  95.             <IFElse:when test="${user==null}"> 
  96.                 大家好! 
  97.             </IFElse:when> 
  98.             <IFElse:otherwise> 
  99.                 我是阳仔! 
  100.             </IFElse:otherwise> 
  101.         </IFElse:choose> 
  102. 日期:20130930 
  103.  
  104. 作者:烟大阳仔 
  105.  
  106. */  
  107. 1.编写三个实现tag接口的JAVA类  
  108. public class IfElseTagLib extends SimpleTagSupport {  
  109.       
  110.     private boolean isDo;  
  111.   
  112.     public boolean isDo() {  
  113.         return isDo;  
  114.     }  
  115.   
  116.     public void setDo(boolean isDo) {  
  117.         this.isDo = isDo;  
  118.     }  
  119.   
  120.     @Override  
  121.     public void doTag() throws JspException, IOException {  
  122.         this.getJspBody().invoke(null);  
  123.     }  
  124.       
  125. }  
  126. public class WhenTag extends SimpleTagSupport   
  127. {  
  128.     private boolean test;  
  129.   
  130.       
  131.     public void setTest(boolean test) {  
  132.         this.test = test;  
  133.     }  
  134.   
  135.   
  136.     @Override  
  137.     public void doTag() throws JspException, IOException {  
  138.         IfElseTagLib parent=(IfElseTagLib) this.getParent();  
  139.         if(test&& !parent.isDo())  
  140.         {  
  141.             this.getJspBody().invoke(null);  
  142.             parent.setDo(true);  
  143.         }  
  144.           
  145.     }  
  146.       
  147. }  
  148. public class otherTag extends SimpleTagSupport {  
  149.   
  150.     @Override  
  151.     public void doTag() throws JspException, IOException {  
  152.           
  153.         IfElseTagLib parent =(IfElseTagLib) this.getParent();  
  154.         if(!parent.isDo())  
  155.         {  
  156.             this.getJspBody().invoke(null);  
  157.             parent.setDo(true);  
  158.         }  
  159.     }  
  160.       
  161.       
  162. }  
  163.       
  164. 2.在tld文件中对标签处理器进行描述(tld文件的位置WEB-INF里面)  
  165. <?xml version="1.0" encoding="UTF-8" ?>  
  166. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
  167.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  168.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"  
  169.     version="2.0">  
  170.     <description>A tag library exercising SimpleTag handlers.</description>  
  171.     <tlib-version>1.0</tlib-version>  
  172.     <short-name>IFElse</short-name>  
  173.     <uri>/IFElse</uri>  
  174.     <tag>  
  175.         <name>choose</name>  
  176.         <tag-class>cn.com.web.ifTag.IfElseTagLib</tag-class>  
  177.         <body-content>scriptless</body-content>  
  178.     </tag>  
  179.     <tag>  
  180.         <name>when</name>  
  181.         <tag-class>cn.com.web.ifTag.WhenTag</tag-class>  
  182.         <body-content>scriptless</body-content>  
  183.         <attribute>  
  184.             <name>test</name>  
  185.             <required>true</required>  
  186.             <rtexprvalue>true</rtexprvalue>  
  187.         </attribute>  
  188.     </tag>  
  189.     <tag>  
  190.         <name>otherwise</name>  
  191.         <tag-class>cn.com.web.ifTag.otherTag</tag-class>  
  192.         <body-content>scriptless</body-content>  
  193.     </tag>  
  194. </taglib>  
  195. 3.在jsp页面中使用标签  
  196. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  197. <%@taglib uri="/IFElse" prefix="IFElse" %>  
  198. <%  
  199. String path = request.getContextPath();  
  200. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  201. %>  
  202.   
  203. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  204. <html>  
  205.   <head>  
  206.     <base href="<%=basePath%>">  
  207.       
  208.     <title>My JSP 'IfElse.jsp' starting page</title>  
  209.       
  210.     <meta http-equiv="pragma" content="no-cache">  
  211.     <meta http-equiv="cache-control" content="no-cache">  
  212.     <meta http-equiv="expires" content="0">      
  213.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  214.     <meta http-equiv="description" content="This is my page">  
  215.     <!--  
  216.     <link rel="stylesheet" type="text/css" href="styles.css">  
  217.     -->  
  218.   
  219.   </head>  
  220.     
  221.   <body>  
  222.         <%  
  223.             session.setAttribute("user""as");  
  224.         %>   
  225.         <IFElse:choose>  
  226.             <IFElse:when test="${user==null}">  
  227.                 大家好!  
  228.             </IFElse:when>  
  229.             <IFElse:otherwise>  
  230.                 我是阳仔!  
  231.             </IFElse:otherwise>  
  232.         </IFElse:choose>  
  233.   </body>  
  234. </html>  
  235.   
  236.   
  237. ----------------------------------------------------------------------------------------------------------  
  238.   
  239. /* 
  240. 案例三:开发一个防盗链标签 
  241.  
  242. 日期:20130930 
  243.  
  244. 作者:烟大阳仔 
  245.  
  246. */  
  247. 1.编写一个实现tag接口的JAVA类  
  248. public class RefererTagLib extends SimpleTagSupport {  
  249.     private String site;  
  250.     private String page;  
  251.     public void setSite(String site) {  
  252.         this.site = site;  
  253.     }  
  254.     public void setPage(String page) {  
  255.         this.page = page;  
  256.     }  
  257.     @Override  
  258.     public void doTag() throws JspException, IOException {  
  259.           
  260.         PageContext pageContext=(PageContext) this.getJspContext();  
  261.         HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();  
  262.         HttpServletResponse response=(HttpServletResponse) pageContext.getResponse();  
  263.         //得到未访问者referer  
  264.         String referer=request.getHeader("referer");  
  265.         if(referer==null||!referer.startsWith(site))  
  266.         {  
  267.             if(page.startsWith(request.getContextPath()))  
  268.             {  
  269.                 response.sendRedirect(page);  
  270.                 return ;  
  271.             }  
  272.             else if(page.startsWith("/"))  
  273.             {  
  274.                 response.sendRedirect(request.getContextPath()+page);  
  275.             }  
  276.             else  
  277.             {  
  278.                 response.sendRedirect(request.getContextPath()+"/"+page);  
  279.             }  
  280.             throw new SkipPageException();  
  281.             //response.sendRedirect(request.getContextPath()+"/index.jsp");  
  282.         }  
  283.     }  
  284.       
  285. }  
  286.   
  287.       
  288. 2.在tld文件中对标签处理器进行描述(tld文件的位置WEB-INF里面)  
  289. <?xml version="1.0" encoding="UTF-8" ?>  
  290. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
  291.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  292.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"  
  293.     version="2.0">  
  294.     <description>A tag library exercising SimpleTag handlers.</description>  
  295.     <tlib-version>1.0</tlib-version>  
  296.     <short-name>RefererTagLib</short-name>  
  297.     <uri>/RefererTagLib</uri>  
  298.     <tag>  
  299.         <name>referer</name>  
  300.         <tag-class>cn.com.web.Referer.RefererTagLib</tag-class>  
  301.         <body-content>empty</body-content>  
  302.         <attribute>  
  303.             <name>site</name>  
  304.             <required>true</required>  
  305.             <rtexprvalue>true</rtexprvalue>  
  306.         </attribute>  
  307.         <attribute>  
  308.             <name>page</name>  
  309.             <required>true</required>  
  310.             <rtexprvalue>true</rtexprvalue>  
  311.         </attribute>  
  312.     </tag>  
  313. </taglib>  
  314. 3.在jsp页面中使用标签  
  315. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  316. <%@taglib uri="/RefererTagLib" prefix="RefererTagLib" %>  
  317. <RefererTagLib:referer site="http://localhost" page="index.jsp"/>  
  318. <%  
  319. String path = request.getContextPath();  
  320. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  321. %>  
  322.   
  323. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  324. <html>  
  325.   <head>  
  326.     <base href="<%=basePath%>">  
  327.       
  328.     <title>Referer</title>  
  329.       
  330.     <meta http-equiv="pragma" content="no-cache">  
  331.     <meta http-equiv="cache-control" content="no-cache">  
  332.     <meta http-equiv="expires" content="0">      
  333.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  334.     <meta http-equiv="description" content="This is my page">  
  335.     <!--  
  336.     <link rel="stylesheet" type="text/css" href="styles.css">  
  337.     -->  
  338.   
  339.   </head>  
  340.     
  341.   <body>  
  342.     This is my JSP page. <br>  
  343.   </body>  
  344. </html>  
  345. ---------------------------------------------------------------------------------------------------------------  

你可能感兴趣的:(三个标签案例:帮你深入学习JSP自定义标签)