自定义标签的一个实例

第一次使用自定义标签,以前喜欢用jsp封装一些功能,达到简化页面的效果。

其实使用自定义标签会使页面逻辑变得很简单,而自定义标签页很简单。

如果你能做出一个例子,我敢说你以后在做页面逻辑,或者架构的一些东西时

肯定会有限考虑自定义标签。现在看看代码吧,多余的知识介绍就不说了。

1.webapp/WEB-INF/menublue.tld

  1. <taglib  
  2.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee web-jsptaglibrary_2_1.xsd"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"> 
  5.     <description>左侧蓝色导航自定义控件标签</description> 
  6.     <tlib-version>1.0</tlib-version> 
  7.     <short-name>ccBuleTagLibrary</short-name> 
  8.     <uri>/ccBuleTagLibrary</uri> 
  9. <tag> 
  10.     <name>menublue</name> 
  11.     <tag-class>org.rd.framework.taglib.MenuBlue</tag-class> 
  12.     <body-content>scriptless</body-content> 
  13.      
  14.      <attribute> 
  15.       <name>menuCategory</name> 
  16.       <required>true</required> 
  17.       <rtexprvalue>true</rtexprvalue> 
  18.     </attribute> 
  19.     <attribute> 
  20.       <name>menuTarget</name> 
  21.       <required>true</required> 
  22.       <rtexprvalue>true</rtexprvalue> 
  23.     </attribute>     
  24.             
  25.   </tag> 
  26.   
  27. </taglib> 
<taglib 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee web-jsptaglibrary_2_1.xsd" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1">
    <description>左侧蓝色导航自定义控件标签</description>
    <tlib-version>1.0</tlib-version>
    <short-name>ccBuleTagLibrary</short-name>
    <uri>/ccBuleTagLibrary</uri>
 <tag>
    <name>menublue</name>
    <tag-class>org.rd.framework.taglib.MenuBlue</tag-class>
    <body-content>scriptless</body-content>
    
     <attribute>
      <name>menuCategory</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>menuTarget</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>    
           
  </tag>
 
</taglib>


2.MenuBlue.java

  1. package org.rd.framework.taglib; 
  2.  
  3. import java.io.IOException; 
  4. import java.util.ArrayList; 
  5.  
  6. import javax.servlet.ServletContext; 
  7. import javax.servlet.http.HttpServletRequest; 
  8. import javax.servlet.http.HttpServletResponse; 
  9. import javax.servlet.http.HttpSession; 
  10. import javax.servlet.jsp.JspException; 
  11. import javax.servlet.jsp.JspWriter; 
  12. import javax.servlet.jsp.tagext.TagSupport; 
  13.  
  14. import org.apache.struts2.ServletActionContext; 
  15. import org.rd.framework.common.container.ContainerManager; 
  16. import org.rd.framework.menu.model.BasResMenu; 
  17. import org.rd.framework.menu.service.MenuBlueService; 
  18.  
  19. public class MenuBlue extends TagSupport{ 
  20.     private MenuBlueService menuBlueService = (MenuBlueService)ContainerManager.getComponent(MenuBlueService.BEAN_ID); 
  21.      
  22.     public int doStartTag() throws JspException { 
  23.         ServletContext sc = (ServletContext) pageContext.getServletContext(); 
  24.         HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); 
  25.         HttpSession sess = (HttpSession) pageContext.getSession(); 
  26.         JspWriter out = pageContext.getOut(); 
  27.         String path = req.getContextPath(); 
  28.         try { 
  29.              
  30.              out.println("<link href=\""+path+"/css/style2.css\" rel=\"stylesheet\" type=\"text/css\" />"); 
  31.              out.println("<script src=\""+path+"/script/jquery-1.7.1.js\"></script>"); 
  32.               
  33.             ArrayList one = new ArrayList(); 
  34.             ArrayList two = new ArrayList(); 
  35.  
  36.             one = menuBlueService.getMenuOneList(this.getMenuCategory(), "1"); 
  37.             two = menuBlueService.getMenuOneList(this.getMenuCategory(), "2"); 
  38.             sess.setAttribute("onemenu", one); 
  39.             sess.setAttribute("twomenu", two); 
  40.  
  41.              
  42.             System.out.println("haha  "+this.getMenuCategory()); 
  43.             System.out.println("hehe  "+this.getMenuTarget()); 
  44.  
  45.             for (int i = 0; i < one.size(); i++) { 
  46.                 BasResMenu bm = (BasResMenu) one.get(i); 
  47.                 String menuId = bm.getMenuId().trim(); 
  48.                 out.println("<div class=\"has_children\">"); 
  49.                 out.println("<div class=\"nbox2\">" + bm.getMenuName() 
  50.                         + "</div>"); 
  51.  
  52.                 if (two.size() > 0) { 
  53.                     for (int k = 0; k < two.size(); k++) { 
  54.                         BasResMenu bmtwo = (BasResMenu) two.get(k); 
  55.                         String parentMenuNo = bmtwo.getParentMenuNo(); 
  56.                         if (parentMenuNo.trim().equals(menuId)) { 
  57.                             String target = ""
  58.                             if (menuTarget != null 
  59.                                     && !menuTarget.trim().equals("")) { 
  60.                                 target = menuTarget
  61.                             } else { 
  62.                                 target = bmtwo.getMenuName(); 
  63.                             } 
  64.  
  65.                             out.println("<h1 class=\"nbox3 blues12\">"); 
  66.                             out.println("<a href=\"" + path+"/" 
  67.                                     + bmtwo.getMenuUrl() + "\" target=\"" 
  68.                                     + target + "\">" + bmtwo.getMenuName() 
  69.                                     + "</a>"); 
  70.                             out.println(" </h1>"); 
  71.  
  72.                         } else { 
  73.  
  74.                         } 
  75.                     } 
  76.                 } 
  77.  
  78.                 out.println("</div> "); 
  79.  
  80.             } 
  81.  
  82.             out.println("<script type=\"text/javascript\">"); 
  83.             out.println("$(\"h1\").hide();"); 
  84.             out.println("$(\".has_children\").click(function(){"); 
  85.             out.println("$(this).children(\"h1\").show().end().siblings().children(\"h1\").hide();"); 
  86.             out.println("});"); 
  87.             out.println("</script>"); 
  88.             out.println(""); 
  89.  
  90.         } catch (IOException e) { 
  91.             e.printStackTrace(); 
  92.         } 
  93.  
  94.         return EVAL_PAGE; 
  95.     } 
  96.  
  97.     public int doEndTag() throws JspException { 
  98.          
  99.         return SKIP_BODY; 
  100.  
  101.     } 
  102.      
  103.     String menuTarget=""
  104.     String menuCategory=""
  105.      
  106.     public String getMenuTarget() { 
  107.         return menuTarget; 
  108.     } 
  109.     public void setMenuTarget(String menuTarget) { 
  110.         this.menuTarget = menuTarget
  111.     } 
  112.  
  113.     public String getMenuCategory() { 
  114.         return menuCategory; 
  115.     } 
  116.  
  117.     public void setMenuCategory(String menuCategory) { 
  118.         this.menuCategory = menuCategory
  119.     } 
package org.rd.framework.taglib;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts2.ServletActionContext;
import org.rd.framework.common.container.ContainerManager;
import org.rd.framework.menu.model.BasResMenu;
import org.rd.framework.menu.service.MenuBlueService;

public class MenuBlue extends TagSupport{
	private MenuBlueService menuBlueService = (MenuBlueService)ContainerManager.getComponent(MenuBlueService.BEAN_ID);
	
	public int doStartTag() throws JspException {
		ServletContext sc = (ServletContext) pageContext.getServletContext();
		HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
		HttpSession sess = (HttpSession) pageContext.getSession();
		JspWriter out = pageContext.getOut();
		String path = req.getContextPath();
		try {
			
		     out.println("<link href=\""+path+"/css/style2.css\" rel=\"stylesheet\" type=\"text/css\" />");
		     out.println("<script src=\""+path+"/script/jquery-1.7.1.js\"></script>");
		     
			ArrayList one = new ArrayList();
			ArrayList two = new ArrayList();

			one = menuBlueService.getMenuOneList(this.getMenuCategory(), "1");
			two = menuBlueService.getMenuOneList(this.getMenuCategory(), "2");
			sess.setAttribute("onemenu", one);
			sess.setAttribute("twomenu", two);

			
			System.out.println("haha  "+this.getMenuCategory());
			System.out.println("hehe  "+this.getMenuTarget());

			for (int i = 0; i < one.size(); i++) {
				BasResMenu bm = (BasResMenu) one.get(i);
				String menuId = bm.getMenuId().trim();
				out.println("<div class=\"has_children\">");
				out.println("<div class=\"nbox2\">" + bm.getMenuName()
						+ "</div>");

				if (two.size() > 0) {
					for (int k = 0; k < two.size(); k++) {
						BasResMenu bmtwo = (BasResMenu) two.get(k);
						String parentMenuNo = bmtwo.getParentMenuNo();
						if (parentMenuNo.trim().equals(menuId)) {
							String target = "";
							if (menuTarget != null
									&& !menuTarget.trim().equals("")) {
								target = menuTarget;
							} else {
								target = bmtwo.getMenuName();
							}

							out.println("<h1 class=\"nbox3 blues12\">");
							out.println("<a href=\"" + path+"/"
									+ bmtwo.getMenuUrl() + "\" target=\""
									+ target + "\">" + bmtwo.getMenuName()
									+ "</a>");
							out.println(" </h1>");

						} else {

						}
					}
				}

				out.println("</div> ");

			}

			out.println("<script type=\"text/javascript\">");
			out.println("$(\"h1\").hide();");
			out.println("$(\".has_children\").click(function(){");
			out.println("$(this).children(\"h1\").show().end().siblings().children(\"h1\").hide();");
			out.println("});");
			out.println("</script>");
			out.println("");

		} catch (IOException e) {
			e.printStackTrace();
		}

		return EVAL_PAGE;
	}

	public int doEndTag() throws JspException {
		
	    return SKIP_BODY;

	}
	
	String menuTarget="";
	String menuCategory="";
	
	public String getMenuTarget() {
		return menuTarget;
	}
	public void setMenuTarget(String menuTarget) {
		this.menuTarget = menuTarget;
	}

	public String getMenuCategory() {
		return menuCategory;
	}

	public void setMenuCategory(String menuCategory) {
		this.menuCategory = menuCategory;
	}
}

定义在标签库的属性要在这个类中也有对应的变量,

这样他自动得到对应的parameter,在本例中的2个打印语句可以打印出值:

haha  dev_level
hehe   ppp

一些说明:

SKIP_BODY隐含0 :跳过了开始和结束标签之间的代码。

EVAL_BODY_INCLUDE隐含1:将body的内容输出到存在的输出流中

SKIP_PAGE隐含5 :  忽略剩下的页面。

EVAL_PAGE隐含6:继续执行下面的页


3,。在jsp页面使用的时候有两种方式

方式1:在jsp页面引入你定义的标签库文件,就直接可以使用,如下:

bb.jsp:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"%> 
  2. <%@ page language="java" pageEncoding="UTF-8" %> 
  3.  
  4. <%@ taglib uri="/WEB-INF/menublue.tld" prefix="cc"%> 
  5.  
  6. <cc:menublue  menuCategory="dev_level" menuTarget="ppp"></cc:menublue> 
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page language="java" pageEncoding="UTF-8" %>

<%@ taglib uri="/WEB-INF/menublue.tld" prefix="cc"%>

<cc:menublue  menuCategory="dev_level" menuTarget="ppp"></cc:menublue>


方式2,:把上面那个uri定义到web.xml中,然后给这个uri起个别名,然后在引入的页面uri就直接可以写别名:

如下:

web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="2.4"  
  3.     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  
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
  7.  
  8.  
  9.     <jsp-config> 
  10.     <taglib> 
  11.       <taglib-uri>thismyselfuri</taglib-uri> 
  12.       <taglib-location>/WEB-INF/menublue.tld</taglib-location> 
  13.     </taglib> 
  14.   </jsp-config> 
  15.  
  16. </web-app> 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	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-app_2_4.xsd">


	<jsp-config>
    <taglib>
      <taglib-uri>thismyselfuri</taglib-uri>
      <taglib-location>/WEB-INF/menublue.tld</taglib-location>
    </taglib>
  </jsp-config>

</web-app>


bb.jsp可以这样写;

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"%> 
  2. <%@ page language="java" pageEncoding="UTF-8" %> 
  3.  
  4. <%@ taglib uri="thismyselfuri" prefix="cc"%> 
  5.  
  6. <cc:menublue  menuCategory="dev_level" menuTarget="ppp"></cc:menublue> 
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page language="java" pageEncoding="UTF-8" %>

<%@ taglib uri="thismyselfuri" prefix="cc"%>

<cc:menublue  menuCategory="dev_level" menuTarget="ppp"></cc:menublue>


这样uri就是定义在web.xml中的,这有什么好处呢?

这样你可以把uri写成服务器的地址+tld的地址,

其实很多tld标签都是这样做的,如果这样写及时你本地没有这个tld标签,程序也会根据这个

uri去访问互联网,保证你的tld标签库可以使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值