[JSP] Custom El functions and using the

Source: Internet
Author: User
Tags tld

Sometimes a series of string processing is required on a JSP page, and a custom El function is required.

First look at the TLD file for the El function:

Standard.jar below:

Custom El Functions:

1. Write the El function ( all public static adornments )

The following function replaces a string with a later format

 Packagecn.xm.exam.MyElFunction;/*** Custom El function, easy to handle some complex string substitution functions in JSP * *@authorQiaoliqiang * @time October 29, 2017 pm 9:09:47*/ Public classmyelfunction {/*** Replace the source string with S1-S2, for example: S1:1234,S2:ABCD to change 1 in source to a,2 for B "* *@paramSource * String that needs to be replaced *@paramS1 * before replacement: 1 2 3 4 5 *@paramS2 * After replacement: A B C D E *@return     */     Public Staticstring Replace (string source, string s1, string s2) { for(inti = 0, length_1 = s1.length (); i < length_1; i++) {Source=Source.replace (S1.charat (i), S2.charat (i)); }        returnsource; }}

2. Write the TLD file description (TLD file in the Web-inf directory, describe the function you just wrote, the tail can refer to Standard.jar/meta-inf/fn.tld)

  TLD file plus unique URI (easy to introduce this URI in JSP)

<?XML version= "1.0" encoding= "UTF-8"?><taglibxmlns= "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">    <Description>JSTL 1.1 Functions Library</Description>    <Display-name>JSTL functions</Display-name>    <tlib-version>1.1</tlib-version>    <Short-name>Myelfunction</Short-name>    <URI>/myfunction</URI>    <function>        <Description>Replace 1234 in the first parameter with ABCD</Description>        <name>Replace</name>        <Function-class>Cn.xm.exam.MyElFunction.MyElFunction</Function-class>        <function-signature>String Replace (java.lang.String, java.lang.String, java.lang.String)</function-signature>        <Example>${replace ("126352", "1234", "ABCD")}</Example>    </function></taglib>

Used in 3.JSP

<%@ Pagelanguage= "Java"ContentType= "text/html; charset=utf-8"pageencoding= "UTF-8"%><% @taglibURI= "/myfunction"prefix= "my"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body>${my:replace ("12345", "1234", "ABCD")}</Body></HTML>

Results:

Use in conjunction with El function in 4.JSP

<%@ Pagelanguage= "Java"ContentType= "text/html; charset=utf-8"pageencoding= "UTF-8"%><% @taglibURI= "/myfunction"prefix= "my"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title><% Request.setattribute("Test", "section");></Head><Body>${my:replace (test, "1234", "ABCD")}</Body></HTML>

Results:

ACB

Parse: The above custom function takes the test out of the field and then replaces the test

[JSP] Custom El functions and using the

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.