生活随笔
收集整理的這篇文章主要介紹了
                                
创建并运用客户化jsp标签
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
 
                                
                            
                            
                            1.在WEB-INF目錄下新建message.properties屬性文件
??????文件內(nèi)容為“key-value”對,添加測試內(nèi)容如下:title=hello world
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?body=hello taglib?
2.定義初始化類TaglibInit,用于加載屬性文件
package com.douyongtao.servlet;
  import java.io.IOException; 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;  public class TaglibInit extends HttpServlet {  public void init(ServletConfig config) throws ServletException { Properties ps = new Properties(); try { ServletContext context = config.getServletContext(); InputStream is = context.getResourceAsStream("/WEB-INF/message.properties"); ps.load(is); is.close(); context.setAttribute("ps", ps); } catch (IOException e) { e.printStackTrace(); } } } 
3.在web.xml中配置該初始化類
<?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"> <servlet> <servlet-name>TaglibInit</servlet-name> <servlet-class>com.douyongtao.servlet.TaglibInit</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 
4.定義標(biāo)簽處理類MyTag
 package com.douyongtao.tag;  import java.io.IOException; import java.util.Properties;  import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport;  public class MyTag extends TagSupport { private String key; public String getKey() { return key; } public void setKey(String key) { this.key = key; }
 @Override public int doEndTag() throws JspException { Properties ps = (Properties) this.pageContext.getAttribute("ps", PageContext.APPLICATION_SCOPE); String message = ps.getProperty(key); try { this.pageContext.getOut().println(message); } catch (IOException e) { e.printStackTrace(); } return EVAL_PAGE; } } 
 
5.在WEB-INF目錄下創(chuàng)建標(biāo)簽庫描述文件MyTag.tld
 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.2</jspversion> <shortname>myTag</shortname> <uri>/myTag</uri>  <tag> <name>message</name> <tagclass>com.douyongtao.tag.MyTag</tagclass> <bodycontent>empty</bodycontent> <attribute> <name>key</name> <required>true</required> </attribute> </tag>  </taglib> 
 
6.創(chuàng)建index.jsp文件并引入標(biāo)簽庫,然后插入標(biāo)簽?
 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/myTag" prefix="hellotag" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head></head> <body> <p> <hellotag:message key="title"/><br/> <hellotag:message key="body"/> </p> </body> </html> 
 ?7.部署應(yīng)用,查看運(yùn)行效果,如果頁面輸出一下內(nèi)容,恭喜你,你成功了
hello world?
hello taglib
 
 
轉(zhuǎn)載于:https://www.cnblogs.com/nakedou/archive/2012/12/16/2820199.html
                            總結(jié)
                            
                                以上是生活随笔為你收集整理的创建并运用客户化jsp标签的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。