随笔-124  评论-49  文章-56  trackbacks-0
自定义函数库
1 定义类和方法(方法必须是public static)
package com.bjsxt.struts;
public class MyFunctios{
  public static String sayHello(String name){
    return "Hello "+name;
  }
}
2 编写自定义tld文件,并且将此文件放在WEB-INF或WEB-INF的任意子目录下
<?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>my functions library</description>
    <display-name>my functions</display-name>
    <tlib-version>1.0</tlib-version>
    <short-name>my</short-name>
    <uri>http://www.bjsxt.com/functions</uri>
    <function>
      <name>say</name>
      <function-class>com.bjsxt.struts.MyFunctions</function-class>
      <function-signature>java.lang.String sayHello(java.lang.String)</function-signature>
    </function>
</taglib>
3 在web.xml中注册(建意,可以不注册)
  <jsp-config>
   <taglib>
    <taglib-uri>http://www.bjsxt.com/functions</taglib-uri>
    <taglib-location>/WEB-INF/my.tld</taglib-location>
   </taglib>
  </jsp-config>
4 在JSP中采用taglib指令引入自定义函数库
<%@ taglib prefix="my" uri="com.bjsxt.struts.MyFunctions"%>
5 调用
${my:say("jack")}
posted on 2009-11-29 22:30 junly 阅读(439) 评论(0)  编辑  收藏 所属分类: jsp/servlet

只有注册用户登录后才能发表评论。


网站导航: