2008-05-12

自定义标签调用资源文件

关键字: 自定义标签调用资源文件
自定义标签处理类一般都继承TagSupport或BodyTagSupport
1.先看一个普通的最简单的自定义标签程序。
A:创建标签的处理类
package com.test.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

@SuppressWarnings("serial")
public class HelloTag extends TagSupport {

	//doStartTag返回的都是BODY相关,doEndTag返回的都是PAGE相关
	@Override
	public int doStartTag() throws JspException {
		try {
			//通过PageContext对象的getOut()方法获得JspWriter对象输出内容
			this.pageContext.getOut().write("hello");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Tag.EVAL_BODY_INCLUDE;
	}

}



B:创建标签库描述文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
	<short-name>hello taglib test</short-name>
	<uri>/hellotag</uri>
	
	<tag>
	     <!--Tag名字 -->
		<name>hello</name>
		<!-- 该Tag对应的处理类 -->
		<tag-class>com.test.tag.HelloTag</tag-class>
		<!-- 该 Tag的body内容为空  -->
		<body-content>empty</body-content>
	</tag>
</taglib>


C.在JSP文件中引入标签库,然后插入标签
jsp里有两种引用方式:
<%@ taglib uri="/WEB-INF/hello.tld" prefix="aa" %>或者
<%@ taglib uri="/hellotag" prefix="aa" %>
<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ taglib uri="/hellotag" prefix="aa" %>


<html>
  <body>
    <aa:hello/>
  </body>
</html>


输出结果为:hello

2.自定义标签库引用资源文件
A:首先在应用程序根目录下建立一个资源文件比如叫:helloworld.properties,内容为:
hello.title=tagTitle
hello.content=tagContent

B:建立一个Servlet,在启动时加载该Servlet,在init方法里读取helloworld.properties文件内容。
package com.test.tag;

import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

@SuppressWarnings("serial")
public class TagServlet extends HttpServlet {
	public void init() throws ServletException {
		System.out.println("init not param");
	}
    //init(ServletConfig config)存在,则init()方法则不会执行。如果init(ServletConfig config)不存在,则init()方法会执行
	@Override
	public void init(ServletConfig config) throws ServletException {
		try{
			System.out.println("init have param");
			Properties p = new Properties();
			//通过ServletContext读取资源文件
			ServletContext context = config.getServletContext();
			InputStream in = context.getResourceAsStream("/helloworld.properties");
			
			p.load(in);
			in.close();
			//设置属性key-value到context中
			context.setAttribute("prop", p);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	

}


C:创建标签库的处理类:
package com.test.tag;

import java.util.Properties;

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

@SuppressWarnings("serial")
public class HelloTagByResource extends TagSupport {
	private String title;
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	//在doStartTag和doEndTag里写都可以。不要误认为:"遇到标签的起始标记时,还没有解析到其属性title,所以放在doStartTag里写会报错"
	@SuppressWarnings("static-access")
	@Override
	public int doStartTag() throws JspException {
		try{
//			Properties p = (Properties)this.pageContext.getAttribute("ps",this.pageContext.APPLICATION_SCOPE);
			Properties p = (Properties)this.pageContext.getAttribute("ps",PageContext.APPLICATION_SCOPE);
			String str = p.getProperty(title);
			this.pageContext.getOut().print(str);
		}catch(Exception e){
			e.printStackTrace();
		}
		return Tag.EVAL_BODY_INCLUDE;
	}
//	@Override
//	public int doEndTag() throws JspException {
//		try{
////			Properties p = (Properties)this.pageContext.getAttribute("ps",this.pageContext.APPLICATION_SCOPE);
//			Properties p = (Properties)this.pageContext.getAttribute("ps",PageContext.APPLICATION_SCOPE);
//			String str = p.getProperty(title);
//			this.pageContext.getOut().print(str);
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		return Tag.EVAL_PAGE;
//	}

	
	
}


D:创建自定义标签的描述文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
	<short-name>hello taglib test</short-name>
	<uri>/hellotag</uri>
	
	<tag>
	     <!--Tag名字 -->
		<name>hello</name>
		<!-- 该Tag对应的处理类 -->
		<tag-class>com.test.tag.HelloTag</tag-class>
		<!-- 该 Tag的body内容为空  -->
		<body-content>empty</body-content>
	</tag>
	
	<tag>
	     <!--Tag名字 -->
		<name>message</name>
		<!-- 该Tag对应的处理类 -->
		<tag-class>com.test.tag.HelloTagByResource</tag-class>
		<!-- 该 Tag的body内容为空  -->
		<body-content>empty</body-content>
		<!-- 属性定义 -->
		<attribute>
			<name>title</name>
			<required>true</required>
		</attribute>
	</tag>
</taglib>

E:jsp里引用
<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ taglib uri="/hellotag" prefix="aa" %>


<html>
  <body>
    <aa:message title="hello.title"/><br/>
    <aa:message title="hello.content"/>
  </body>
</html>



输出内容为:
tagTitle
tagContent
评论
发表评论

您还没有登录,请登录后发表评论

ttitfly
搜索本博客
我的相册
63e97aa5-d2de-33be-88c3-8f39d5279b8d-thumb
bbbb
共 3 张
存档
最新评论