JSTL1.1 function Tag Library (functions) such as fn:length and JSTL Custom Function function tags __ function

Source: Internet
Author: User
Tags tld

The FN tag in the JSTL is also a key tag that we often use in web design, with a header first

<%@ taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "FN"%> can use the FN tag.

Refer to the following table for specific uses:

function

Description

Fn:contains (string, substring)

Returns true if parameter substring is included in the parameter string

Fn:containsignorecase (string, substring)

Returns true if the parameter string contains the parameter substring (ignore case)

Fn:endswith (string, suffix)

Returns true if the argument string ends with the argument suffix

Fn:escapexml (String)

Converts XML (and HTML) with special meaning to the corresponding XML character entity code, and returns

Fn:indexof (string, substring)

Returns the position of the first occurrence of the parameter substring in the parameter string

Fn:join (array, separator)

Strings a given array of arrays with a given spacer separator, forming a new string and returning.

Fn:length (item)

Returns the number of elements contained in the parameter item. The parameter item type is an array, collection, or string. If it is a string type, the return value is the number of characters in string.

Fn:replace (String, before, after)

Returns a String object. Replaces all occurrences of the parameter before string in the parameter string with the argument after string and returns the result of the substitution

Fn:split (string, separator)

Returns an array with the parameter separator as the separator string, and each part of the partition is an element of the array

Fn:startswith (string, prefix)

If the argument string is prefix with a parameter

Begins, returns true

Fn:substring (string, begin, end)

Returns the string part of the argument string, starting at the parameter end, including the character

Fn:substringafter (string, substring)

Returns the portion of the string that follows the parameter substring in the parameter string

Fn:substringbefore (string, substring)

Returns the part of the string that precedes the argument substring in the parameter string

Fn:tolowercase (String)

Changes all characters in the parameter string to lowercase and returns them to the

Fn:touppercase (String)

Capitalizes all characters in the parameter string and returns them to the

Fn:trim (String)

Remove the trailing space of the parameter string and return it

function functions in custom labels for taglib labels in custom JSPs

What can <function> tag do? It allows us to call a method directly in the JSP, return the specified value according to the custom method, and the compatible JSTL tag, eliminating the direct use of the <%!%> in the JSP To define the complexity of the method body invocation. If you use El language, you'll probably get started quickly, but <function> tag is an El language with a method body. Note: The method defined by the function must be static if it is not static jstl Is that the defined method is not recognized.

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 ();
	}
}

The method must be static, and you can define a method that has a return value or a void type.

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 invoke name--> <name>hello</name> <!--Specify the processing class for the label, specifying which Java class The label is to handle. --> <function-class>org.lxh.taglib.FunctionTag</function-class> <!-- Specifies the method that is actually called in the name of the El page call. Specifies the actual method to process the class. Parameters and callback functions to write the full path--> <function-signature>java.lang.string Hello ( java.lang.String) </function-signature> </function> <function> <name>bbq</name> <f Unction-class>org.lxh.taglib.functiontag</function-class> <function-signature>java.lang.integer BBQ (java.util.List) </function-signaTure> </function> </taglib> 

Note: In <function-signature> need to write the complete class name, if it is a string type must write java.lang.String such words, does not support the definition of generics such as java.util.list< Java.lang.string>eclipse will judge <> as an XML format, so omit the definition of the generic.

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.*"%><meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<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>

Note: When invoking a method, the type must pass in the same type of value, otherwise the error will be, but for the method body is string, you can pass in the List,set,map those, because the pass-through will call the list's. ToString () method directly when the string is output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.