Function function in Taglib tag in custom JSP

 

java code:

package org.lxh.taglib;

import java.util.List;

public class FunctionTag {

	public static String hello(String name) {

		return name;
	}

	public static Integer bbq(List list) {

		return list.size();
	}
}

 Methods must be static, and methods with return values ​​or void types can be defined.

 

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">
	<tlib-version>1.0</tlib-version>
	<short-name>my</short-name>
	<uri>http://lauedward.spaces.live.com</uri>
	<function>
		<!--EL page call name -->
		<name>hello</name>
		<!--Specifies the processing class of the tag, and specifies which Java class the tag is processed by. -->
		<function-class>org.lxh.taglib.FunctionTag</function-class>
		<!--Specify the method actually called in the name of the EL page call. Specify the actual method of the processing class. The parameters and callback functions should be written with the full path-->
		<function-signature>java.lang.String hello(java.lang.String)</function-signature>

	</function>

	<function>
		<name>bbq</name>
		<function-class>org.lxh.taglib.FunctionTag</function-class>
		<function-signature>java.lang.Integer bbq(java.util.List)</function-signature>
	</function>
</taglib>

 Note: In <function-signature>, you need to write the complete class name. If it is a String type, you must write the word java.lang.String. Generic definitions such as java.util.List<java.lang.String> are not supported. Eclipse will judge <> as the format of xml, so the definition of the generic type is omitted.

 

jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="my" uri="/WEB-INF/tld/testFunction.tld"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.util.*"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
    List<String> list = new ArrayList<String>();
    list.add("aa");
    list.add("bb");
    list.add("cc");
    request.setAttribute("list", "helloword");
    request.setAttribute("name", list);
    Map map = new HashMap();
    map.put("1","a");
    map.put("2","b");
    map.put("3","c");
    map.put("4","d");
%>

<br>
${my:hello(list)}
<br>
${my:bbq(name)}
<br>

</body>
</html>

 Note: When calling a method, you must pass in the same type of value, otherwise an error will be reported, but if the method body is String, you can pass in list, set, map, because list will be called directly after passing in. The toString() method directly outputs a string.

 

 

Source: http://www.cnblogs.com/edwardlauxh/archive/2010/05/19/1918589.html

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326342534&siteId=291194637