Posted on 2007-10-21 12:43
疯狂 阅读(633)
评论(0) 编辑 收藏
首先建立自己的函数:
package com.struts;
public class MyFunction {
public static String welcome(String user){
String welcome="welcome "+user;
return welcome;
}
}
然后在项目的web-inf下创建一个tld文件myFunction.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>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>//方法,返回值,参数类型
</function>
</taglib>
然后是测试页面:test_myfunction.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="myfunction" uri="http://com.struts/myfunctions" %>//引入自定义函数
<!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表达式调用方法
</body>
</html>