Taglib tag four in custom JSP Function function in custom tag

The example has been written before, but it has not been released due to time constraints. This time, the basic functions of <tag> in taglib have been introduced. In taglib, we found that there is a tag called <function>, this time Briefly introduce the basic usage of the <function> tag. What can the <function> tag do? It allows us to directly call a method in jsp, and return the specified value according to the custom method. It is compatible with the jstl tag and saves the It is cumbersome to use <%!%> directly in jsp to define the method body and call it again. If you have used the el language, it is estimated that you will get started quickly. In fact, the <function> tag is an el language with a method body. Note: the definition of function The method must be static. If it is not static, jstl cannot recognize the defined method.

The Java code is as follows:

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 > 
		<!-- The processing class of the specified tag , which specifies which Java class handles the tag. --> 
		< 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 completely path --> 
		< function - signature > java.lang.String hello(java.lang.String) <

	</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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326685963&siteId=291194637