Jstl truncation character length Processing

Source: Internet
Author: User
Tags tld

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>

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.