Jstl:
<C: Set Var = "substr" value = "abcdefghijksdf"
<C: Choose>
<C: When test = "$ {fn: length (substr)> 2}">
<C: Out value = "$ {fn: substring (substr, 0, 2)} ......"/>
</C: When>
<C: otherwise>
<C: Out value = "$ {substr}"/>
</C: otherwise>
</C: Choose>
Note: Remember to reference the tag Library
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "FN" %>
Ii. User-Defined Functions
Package com. uisk. util;
/**
* Define functions
*/
Public class ufunction {
/**
* Get the length of a string
*
* @ Param Str
* @ Return int
*/
Public static int getlen (string Str ){
Return Str. Length ();
}
/**
* Truncate a string
*
* @ Param Str
* @ Param start
* @ Param end
* @ Return string
*/
Public static string substr (string STR, int start, int end ){
Return Str. substring (START, end );
}
/**
* Division of two numbers to obtain the integer result
*
* @ Param first
* @ Param second
* @ Return int
*/
Public static int Chufa (INT first, int second ){
Return first/second;
}
}
Create a TLD file and place it in the WEB-INF directory to configure it:
<? 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> Len </short-Name>
<Function>
<Description> calculate String Length </description> <! -- Description of this El Method -->
<Name> getlen </Name> <! -- Call the El method name -->
<Function-class> com. CW. Common. ufunction </function-class>
<Function-signature>
Int getlen (Java. Lang. String)
</Function-signature>
<Example >$ {Len: getlen (STR) }</example> <! -- For example -->
</Function>
<Function>
<Description> substring </description>
<Name> substr </Name>
<Function-class> com. CW. Common. ufunction </function-class>
<Function-signature>
Java. Lang. String substr (Java. Lang. String, Int, INT)
</Function-signature>
</Function>
<Function>
<Description> Chufa </description>
<Name> Chufa </Name>
<Function-class> com. CW. Common. ufunction </function-class>
<Function-signature> int Chufa (INT, INT) </function-signature>
</Function>
</Taglib>
Finally, perform the call test:
<% @ Page Language = "Java" pageencoding = "GBK" %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>
<% @ Taglib uri = "/WEB-INF/mytag. TLD" prefix = "mytag" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Body>
<C: Set Var = "str" value = "user-defined function, to test string length truncation"> </C: Set>
The length of the string "$ {STR}" is $ {mytag: getlen (STR)} <br/>
String "$ {STR}": $ {mytag: substr (STR,)} <br/>
5 divided by 3, the integer is $ {mytag: Chufa (5, 3 )}
</Body>
</Html>