日期:2014-05-16  浏览次数:20444 次

JSP自定义标签实现数据字典

?

1.关于JSP标签的好处就不再罗嗦

数据字典就是使用的下拉框,只要定义使用那个字典就会将这个字典可用的内容显示出来

显示字典时只要定义那个字典和属性值就可以显示出字典的显示值

?

2.首先在web.xml中定义自定义标签加载的引用,两个属性分别是引用的URI和加载路径

?

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<jsp-config>
		<taglib>
			<taglib-uri>/tld/web-html</taglib-uri>
			<taglib-location>
				/WEB-INF/tlds/web-html.tld
			</taglib-location>
		</taglib>
	</jsp-config>
</web-app>

?

?

?

3.在web-html.tld中定义自己的标签,数据字典应用的话我们需要一个标签库,三个标签。分别是,select标签,options标签,和现实数据字典的标签,每个标签都对应不同的实现类

?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
	"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
	<tlib-version>1.0</tlib-version><!-- 标签库版本 -->
	<jsp-version>1.2</jsp-version>  <!-- 标签库要求的JSP规范版本 -->
	<short-name>html</short-name>   <!-- JSP页面编写工具可以用来创建助记名的可选名字 -->
	<tag>
		<name>select</name>
		<tag-class>com.SelectTag</tag-class>
		<body-content>JSP</body-content>
		<attribute>
			<name>name</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>style</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
	<tag>
		<name>options</name>
		<tag-class>com.OptionsTag</tag-class>
		<body-content>JSP</body-content>
		<attribute>
			<name>collection</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
	<tag>
		<name>selectDisplay</name>
		<tag-class>com.SelectDisplay</tag-class>
		<body-content>JSP</body-content>
		<attribute>
			<name>collection</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>name</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>value</name>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
</taglib>

?

?

?

?

4.实现类

实现类的作用就是在后台拼接所需HTML标签内容,然后由JSP进行输出

实现类最主要的两个方法,一个遇到这个标签开始时输出,一个是结束时输出

如果需要定义属性,可以参考实现类定义属性,并在TLD中定义,在JSP中使用标签时快捷键就可以出来这个属性

首先是select标签的代码:

?

package com;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
 * TagSupport与BodyTagSupport的区别:
 * 主要看标签处理类是否要读取标签体的内容和改变标签体返回的内容,如果不需要就用TagSupport,否则就用BodyTagSupport
 * 用TagSupport实现的标签,都可以用BodyTagSupport来实现,因为BodyTagSupport继承了TagSupport
 */
@SuppressWarnings("serial")
public class SelectTag extends BodyTagSupport {
	@Override
	public int doStartTag() throws JspException {
		try {
			StringBuffer results = new StringBuffer("<select");
			if(name != null){
	            results.append(" name=\"");
	            results.append(name);
	            results.append("\"");
	        }
			if(style != null){
	            results.append(" style=\"");
	            results.append(style);
	            results.append("\"");
	        }
			results.append(">");
			pageContext.getOut().write(results.toString());
		} catch (IOException ex) {
			throw new JspTagException("错误");
		}
		return EVAL_BODY_INCLUDE;
	}
	@Over