jsp自定义标签

自定义标签是用户定义的JSP语言元素。当JSP页面包含一个自定义标签时将被转化为servlet,标签转化为对被 称为tag handler的对象的操作,即当servlet执行时Web container调用那些操作。

JSP标签扩展可以让你创建新的标签并且可以直接插入到一个JSP页面。 JSP 2.0规范中引入Simple Tag Handlers来编写这些自定义标记。

注意:自定义标签类是在spring容器初始化前加载的,因此通常来说,是不能注入spring容器管理的类(可以通过继承RequestContextAwareTag来实现引用spring管理的类)。

当页面执行到自定义标签时,会通过tld文件中的配置,找到对应的实现类,来最终得到结果进行页面渲染,如渲染失败会导致页面报错。

此处我们实现的功能是页面的面包屑标签的定义,不需要使用spring管理的类,因此继承的是TagSupport类,重写doStartTag方法。

首先建立.tld文件:

<?xml version="1.0" encoding="UTF-8" ?> 
<taglib 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-jsptaglibrary_2_0.xsd" 
version="2.0"> 
<description>信用平台通用设置</description> 
<display-name>permission core</display-name> 
<tlib-version>1.0</tlib-version> 
<short-name>system</short-name> 
<uri>http://www.lnlic.com</uri> 
<tag>
	<description>面包屑标签</description> 
	<name>breadcrumbsSetting</name>
	<tag-class>com.lnlic.credit.security.BreadcrumbsTag</tag-class>
	<body-content>JSP</body-content>
	<attribute>
		<name>breadcrumbName</name>
		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
	</attribute>
</tag>
</taglib> 

然后建立自定义标签实现类: 

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.lnlic.credit.common.core.constant.portal.BreadcrumbsConstant;

/**
 * <p>面包屑标签</p>
 * @ClassName: BreadcrumbsTag
 */
public class BreadcrumbsTag extends TagSupport implements Serializable {
	private static final long serialVersionUID = 6958512489709325566L;
	
	private final static Logger logger = Logger.getLogger(BreadcrumbsTag.class);
	
	// 菜单名
	private String breadcrumbName;
	public String getBreadcrumbName() {
		return breadcrumbName;
	}
	public void setBreadcrumbName(String breadcrumbName) {
		this.breadcrumbName = breadcrumbName;
	}

	@Override
	public int doStartTag() throws JspException {
		// 真:返回EVAL_BODY_INCLUDE(执行标签);假:返回SKIP_BODY(跳过标签不执行)
		PageContext pg = super.pageContext;
		JspWriter out = pg.getOut();
		try {
			// 接收页面传参,获取页面传入的面包屑名称
			String[] menus = breadcrumbName.split(",");
			// 获取菜单配置,获取配置文件中配置的面包屑,key为面包屑名称,value为面包屑路径
			Map<String, String> menuMap = BreadcrumbsConstant.BREADCRUMBS;
			for (int i = 0; i < menus.length; i++) {
				//通过页面传入的面包屑名称作为key,获取配置文件中对应的面包屑路径
				String menuUrl = menuMap.get(menus[i]);
				boolean isLast = i==menus.length-1;
				// 不为空说明在配置文件中存在,则进行面包屑拼接
				if (!StringUtils.isEmpty(menuUrl)) {
					// 如果是最后一级节点,则无跳转链接
					out.write("<a href=\""+(isLast?"javascript:void(0)":menuUrl)+"\" target=\"_self\" class=\"f_black1 no_unl\">"+menus[i]+"</a>");
				} else {
					// 如果未配置此菜单,则提示
					out.write("<a href=\"javascript:void(0)\" target=\"_self\" class=\"f_black1 no_unl\" style=\"color:red\">"+menus[i]+" (此菜单不存在!)</a>");
				}
				if (i != menus.length-1) {
					out.write(" &nbsp;&gt;&nbsp; ");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return EVAL_BODY_INCLUDE;
	}
	
}

面包屑常量类:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;

/**
 * <p>面包屑常量类</p>
 * @ClassName: BreadcrumbsConstant
 */
public class BreadcrumbsConstant {
	
	private final static Logger logger = Logger.getLogger(BreadcrumbsConstant.class);

	/**
	 * 以map形式存储面包屑配置,key为面包屑名称,value为对应的路径
	 */
	public static HashMap<String, String> BREADCRUMBS;
	
	/**
	 * 配置文件路径
	 */
	private static final String SETTINGPATH = "portal/breadcrumbs.properties";

	private static Properties props = null;

	static {
		try {
			if (BREADCRUMBS == null) {
				BREADCRUMBS = new HashMap<String, String>();
			}
			props = new Properties();
			InputStream inputStream = BreadcrumbsConstant.class.getClassLoader().getResourceAsStream(SETTINGPATH);
			BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
			props.load(bf);
//			props = PropertiesLoaderUtils.loadAllProperties(SETTINGPATH);
			Set<Object> keys=props.keySet();
			for (Object k : keys) {
				BREADCRUMBS.put((String)k, (String)props.get(k));
			}
		} catch (Exception e) {
			//e.printStackTrace();
			logger.error("breadcrumbs.properties 未找到!!!");
		}
	}

}

面包屑配置文件信息breadcrumbs.properties:

首页=http\://xysy.shenyang.gov.cn
信用信息查询=http\://xysy.shenyang.gov.cn\:8282/credit-web-portal/website/wsdataquery/index
信用公示=http\://xysy.shenyang.gov.cn\:8282/credit-web-portal/website/wsreportingdoublepublicityquery/detail
信用双公示=asdasd
双公示=http\://xysy.shenyang.gov.cn\:8282/credit-web-portal/website/webreportingdoublepublicityquery/index
行政许可=#
行政处罚=#

使用方式,在页面引入tld文件,并命名:

在jsp页面中使用:

最终效果:

以上为常规的自定义标签使用方式, 还有一种情况是,我们需要调用请求数据库的业务接口service,此时就需要通过继承RequestContextAwareTag来实现了。详情请看下篇文章

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值