【Java.Web】JSP —— 标签库taglib —— 自定义JSP标签 —— 示例

示例1 - 访问自定义标签的属性


创建message标签的处理类MessageTag

创建一个包含JSP网页静态文本的资源文件

在应用的WEB-INF目录下,创建一个资源文件messageresource.properties,

#
hello.title = hello title
hello.body = hello body

在Web应用启动时加载静态资源文本

创建一个LoadServlet类,利用其init()方法来完成静态资源文本的加载。将它们加载到对象的Properties对象中,然后把这个对象作为属性保存到Web应用范围内:

package com.gof.test.servlet;

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

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

public class LoadServlet extends HttpServlet {
	
    public void init() throws ServletException{
    	Properties ps = new Properties();
    	InputStream in = null;
    	
    	try{
    		ServletContext sc = getServletContext();
    		in = sc.getResourceAsStream("/WEB-INF/messageresource.properties");
    		ps.load(in);
    		in.close();
    		
    		sc.setAttribute("messageresource", ps);
    		
    	} catch (Exception e){
    		e.printStackTrace();
    	}finally{
    		try{
    			in.close();
    		}catch (Exception e){
    			e.printStackTrace();
    		}
    	}
    }
}

在web.xml文件中注册LoadServlet,设置其<load-on-startup>元素,保证启动时加载:

  <servlet>
  	<servlet-name>load</servlet-name>
  	<servlet-class>com.gof.test.servlet.LoadServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>load</servlet-name>
      <url-pattern>/load</url-pattern>
  </servlet-mapping>

创建MessageTag类

package com.gof.test.taglib;

import java.util.Properties;

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

public class MessageTag extends TagSupport {
    private String key = null;
    
    public String getKey(){
    	return this.key;
    }
    
    public void setKey(String key){
    	this.key = key;
    }
    
    public int doEndTag() throws JspException{
    	try{
    		Properties ps = (Properties)pageContext.getAttribute("messageresource", PageContext.APPLICATION_SCOPE);
    		if (ps != null){
    		    String message = null;
    		    message = ps.getProperty(key);
    		    pageContext.getOut().print(message);		
    		}
    	}catch (Exception e){
    		e.printStackTrace();
    	}
    	
    	return EVAL_PAGE;
    }
}<span style="font-family:SimSun;font-size:10px;">
</span>


该标签处理类包含一个属性key,用于保存key的值,当标签结束时,根据key的值从资源文件中读取对应的文本值,然后写入到输出流中。


创建标签库描述文件

创建一个标签库描述文件mytaglib.tld:

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns:xml="http://www.w3.org/XML/1998/namespace" 
	                  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-jsptaglibrary_2_1.xsd ">
  <tlib-version>1.1</tlib-version>
  <jsp-version>2.1</jsp-version>
  <short-name>mytaglib</short-name>
  <uri>/mytaglib</uri>
  
  <tag>
      	<name>message</name>
      	<tag-class>com.gof.test.taglib.MessageTag</tag-class>
      	<body-content>empty</body-content>
      	<attribute>
      		<name>key</name>
      		<required>true</required>
      	</attribute>
  </tag>

</taglib>


在Web应用中使用标签

在web.xml中加入<taglib>元素,声明所引用的标签库:

  <!-- taglib configuration -->
  <jsp-config>
      <taglib>
          <taglib-uri>/mytaglib</taglib-uri>
          <taglib-location>/WEB-INF/mytaglib.tld</taglib-location>
      </taglib>
  </jsp-config>


在jsp文件中哦个通过<%@ taglib %> 指令声明对标签库的引用:

taglibtest0.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="/mytaglib" prefix="mm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <mm:message key="hello.title" />
    <br/>
    <mm:message key="hello.body" />
    <br/> 
</body>
</html>

在浏览器中访问上面的JSP文件:

http://localhost:8080/base-webapp/jsp/tagjsp/taglibtest0.jsp








你可能感兴趣的:(java,Web)