`
lushuaiyin
  • 浏览: 676355 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

自定义标签的一个实例

 
阅读更多

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

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

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

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

1.webapp/WEB-INF/menublue.tld

<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

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:

<%@ 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:

<?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可以这样写;

<%@ 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标签库可以使用。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics