JSP Custom Method Library

Source: Internet
Author: User

If the Jstl method library does not meet your needs, you can use a custom method to extend

public class function{public      static int Length (object obj) {  //returns the length of the object             if (obj = = null) {                    return 0;             }              if (obj instanceof stringbuffer) {                    return Lenth (((stringbuffer) obj). toString ());             }              if (obj instanceof String) {                    return ((String) obj). getbytes.length;             }              if (obj instanceof Collection) {                    return ((Collection) obj). Size ();}}      }

Configuration file

<?xml version= "1.0" ecoding= "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 Web-jsptaglibr Ary_2_0.xsd version= "2.0" > <description>custom functionslibrary</description> <display-n Ame>customfunctions</display-name> <tlibversion>1.1</tlibversion> <shortname>function </shortname> <uri>http://www.clf.com/tags</uri> <function> <description >descriptioncontent </description> <name>lenth</name> <function-class>c om.chen.funtion</function-class> <function-signature> int Length (java.lang.Obje             CT) </function-signature> <example> ${fn:length (String)} </example> <fuNction> </taglib> 

The declaration of the custom method is written in the <function-signature> tag, in the form

The return value method name (parameter 1 type, parameter 2 type ...) )

Add a string variable, assign "string test"

The tag library defined in the JSP page is prefixed with "FN", then use the following

${fn:length (String)}

This sentence will be able to output the length of the string


JSP Custom Method 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.