jsp自定義標籤的使用

1.首先我們先來創建一個類,用標籤來實現類裏面的方法,該方法必須爲靜態方法

public class Text {
    public static String mytag(){
    	String str="這是一個自定義標籤。。。";
    	return str;
    }
}
2.第二步創建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>JSTL 1.1 functions library</description>
  <display-name>JSTL functions</display-name>
  <tlib-version>1.1</tlib-version>代表標籤庫的版本號
  <short-name>mytag</short-name>自定義標籤名
  <uri>http://www.mytag.com</uri>自定義連接
   <function>
    <description>
     mytag      標籤描述
    </description>
    <name>mytag</name>  標籤名
    <function-class>zhidingyi.Text</function-class>   類的路徑
    <function-signature>java.lang.String mytag() </function-signature>   返回值類型和方法名
    
  </function>
  </taglib>
3.前臺頁面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="mytag" uri="http://www.mytag.com" %>引入標籤庫


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    ${mytag:mytag() } 用el表達式輸出
  </body>
</html>

前臺頁面顯示








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章