瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          struts中自定義taglib函數(shù)例子

          Posted on 2007-10-21 12:43 瘋狂 閱讀(641) 評論(0)  編輯  收藏


          首先建立自己的函數(shù):
          package com.struts;

          public class MyFunction {
             public static String welcome(String user){
              String welcome="welcome  "+user;
              return welcome;
             }
          }
          然后在項目的web-inf下創(chuàng)建一個tld文件myFunction.tld用來注冊函數(shù)內(nèi)容如下:
          <?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>myfunction</description>
            <display-name>myfunction</display-name>
            <tlib-version>1.0</tlib-version>
            <short-name>myfunction</short-name>
            <uri>http://com.struts/myfunctions</uri>//定義一個資源地址

            <function>
              <name>welcome</name>
              <function-class>com.struts.MyFunction</function-class>//類
              <function-signature>java.lang.String welcome(java.lang.String)</function-signature>//方法,返回值,參數(shù)類型
            </function>
            </taglib>
          然后是測試頁面:test_myfunction.jsp

          <%@ page language="java" contentType="text/html; charset=GB18030"
              pageEncoding="GB18030"%>
          <%@ taglib prefix="myfunction" uri="http://com.struts/myfunctions" %>//引入自定義函數(shù)
          <!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=GB18030">
          <title>test myFunction</title>
          </head>
          <body>
          <h1>test_myfunction</h1><hr color="blue">
          <li>test-jstl-myFunction</li><br>
          (use-myfunction):<font color="red">${myfunction:welcome("Tom")}</font><br>//用el表達式調(diào)用方法

          </body>
          </html>


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 长沙县| 旺苍县| 左云县| 苍山县| 长治县| 邵东县| 井研县| 松原市| 绩溪县| 宿松县| 安溪县| 鹤壁市| 丰顺县| 清镇市| 甘谷县| 平利县| 五家渠市| 白城市| 晋州市| 廉江市| 合肥市| 隆化县| 娱乐| 丹棱县| 石台县| 台中市| 长宁县| 阿拉善盟| 长白| 瑞金市| 班戈县| 临城县| 清镇市| 洛浦县| 满洲里市| 凤凰县| 丹棱县| 拉萨市| 平安县| 乡宁县| 襄垣县|