[Application] Sixth JSTL Custom Function tag Library

Source: Internet
Author: User
Tags tld

The function tag Library has been explained in the previous JSTL's summary, and I'll rearrange it once again!

Introduction to the self-brought function tag library

The way to introduce this tag library is:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Name of function function Description Examples of Use
fn:contains Determine if a string contains another string <c:if test="${fn:contains(name, searchString)}">
fn:containsIgnoreCase Determines whether a string contains another string (case-insensitive) <c:if test="${fn:containsIgnoreCase(name, searchString)}">
fn:endsWith Determines whether a string ends with a different string <c:if test="${fn:endsWith(filename, ".txt")}">
fn:escapeXml Turn some characters into XML representations, such as < characters that should be converted to&lt; ${fn:escapeXml(param:info)}
fn:indexOf The position where the substring appears in the parent string ${fn:indexOf(name, "-")}
fn:join Unites the data in the array into a new string and uses the specified character format to open the ${fn:join(array, ";")}
fn:length gets the length of the string, or the size of the array ${fn:length(shoppingCart.products)}
fn:replace Replace the character specified in the string ${fn:replace(text, "-", "&#149;")}
fn:split Segmentation the string according to the specified word ${fn:split(customerNames, ";")}
fn:startsWith Determines whether a string starts with a substring <c:if test="${fn:startsWith(product.id, "100-")}">
fn:substring Intercept sub-strings ${fn:substring(zip, 6, -1)}
fn:substringAfter Gets the substring starting at the location of a character ${fn:substringAfter(zip, "-")}
fn:substringBefore Gets the substring from the beginning to the location of a character ${fn:substringBefore(zip, "-")}
fn:toLowerCase Convert to lowercase ${fn.toLowerCase(product.name)}
fn:toUpperCase Convert to uppercase ${fn.UpperCase(product.name)}
fn:trim Remove spaces before and after a string ${fn.trim(name)}

Explain

  • Fn:contains (string, substring) returns true if the argument string contains a parameter substring
  • Fn:containsignorecase (string, substring) returns true if the argument string contains a parameter substring (ignoring 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 together with the given spacer separator, forming a new string and returning it.
  • Fn:length (item) returns the number of elements contained in the parameter item. The parameter item type is an array, a collection, or a string. If it is of type string, the return value is the number of characters in string.
  • Fn:replace (String, before, after) returns a String object. Replaces all occurrences of a parameter before string in a parameter string with a parameter after string, and returns the replaced result
  • Fn:split (string, separator) returns an array that splits the argument string with the parameter separator, and each part of the split is an element of the array
  • Fn:startswith (string, prefix) if the argument string starts with the argument prefix, returns true
  • Fn:substring (string, begin, end) returns the string of the argument string, starting with the argument begin to the argument end position, including the character of the end position
  • Fn:substringafter (string, substring) returns the argument substring the part of the string that follows the argument string
  • Fn:substringbefore (string, substring) returns the part of the string that precedes the argument substring in the argument string
  • Fn:tolowercase (string) turns all the characters in the argument string to lowercase and returns
  • Fn:touppercase (string) converts all characters of the argument string to uppercase and returns it
  • Fn:trim (string) strips the trailing and trailing spaces of the parameter string and returns
Custom Function Libraries The first step is to customize the class and method (public +static)
  1. package com.pangsir.ty;
  2. publicclassTestFunction{
  3. /**
  4. *
  5. * 自定义类和方法 ,方法必须是public + static
  6. * @param name
  7. * @return
  8. */
  9. publicstaticString toTest(String name){
  10. return"练习自定义函数, "+name;
  11. }
  12. }

Note: Totest () must be public static .

The second step is to write the custom tldFile, place this custom TLD file in the WEB-INFOr WEB-INF的任意子目录Under
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <taglibxmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  5. version="2.0">
  6. <!--该文件主要是受这个的影响:http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd -->
  7. <description>test own defined functions library</description>
  8. <display-name>test functions</display-name>
  9. <!-- 定义的版本 -->
  10. <tlib-version>1.0</tlib-version>
  11. <!--这个名字可以随便取,尽量与文件名相同,这样我们知道文件在哪儿 -->
  12. <short-name>test</short-name>
  13. <!-- 这个地址是随便取得。到时候jsp页面引入这个地址 -->
  14. <uri>http://codemihong.com/functions</uri>
  15. <!-- 定义函数 -->
  16. <function>
  17. <!-- 自定义函数名称(随意) -->
  18. <name>testFunction</name>
  19. <!-- 定义函数的类全名称 -->
  20. <function-class>com.pangsir.ty.TestFunction</function-class>
  21. <!--java.lang.String:返回值类型 toTest:类定义的函数名(形参的数据类型) -->
  22. <function-signature>java.lang.String toTest(java.lang.String)</function-signature>
  23. </function>
  24. </taglib>

Description

    1. <description>、<description-name>、<tlib-version>、<short-name>、<uri>the label content is 注意这里设置的uri后面jsp中要引入 arbitrary,.
    2. <function>tags in the tag <name> in the JSP to be called as function name, where the name can be consistent with the actual function name, can also be inconsistent, can be considered an alias of the actual function name.
    3. <function-class>The tag is the package name + Class name (custom Class).
    4. <function-signature>is a description of the custom function, if it is a wrapper type, write the full path, or if it is the base data type, it is not required . This is shown in another example:
  1. <function>
  2. <description>
  3. Returns the index withing a string of the first occurrence of a specified substring.
  4. </description>
  5. <name>indexOf</name>
  6. <function-class>org.apache.taglibs.standard.functions.Functions</function-class>
  7. <function-signature>int indexOf(java.lang.String, java.lang.String)</function-signature>
  8. <example>
  9. ${fn:indexOf(name, "-")}
  10. </example>
  11. </function>

补充说明:Register the JSTL function, and if the URI is/web-inf/xxx.tld, you do not need to register the following Tomcat

    1. <!-- 注册JSTL函数 -->
    2. <jsp-config>
    3. <taglib>
    4. <taglib-uri>http://codemihong.com/functions</taglib-uri>
    5. <taglib-location>/WEB-INF/myfunctions.tld</taglib-location>
    6. </taglib>
    7. </jsp-config>
The third step is to introduce a JSP page in which you want to use your custom function library

<%@ taglib prefix="hp" uri="http://codemihong.com/functions" %>

The fourth step uses: ${prefix + colon + function alias}
    1. <body>
    2. 测试JSTL--自定义函数库
    3. <spanstyle="color:#006600;"></span> ${hp:testFunction("人总是需要向前看的") }
    4. </body>
Fifth Step Test



[Application] Sixth JSTL Custom Function tag Library

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.