Detailed description of custom El expressions

Source: Internet
Author: User
Tags tld

In Java, we can directly write string STR = str1 + str2 in this way. In JSP, if the El expression is used, ${1 + 2} returns 3, $ {'xxx' + 'xxx'} is incorrect, because El calculates it and throws an exception in Java. lang. numberformatexception: for input string: "XXX", but many times we may have this requirement. The following describes how to write a custom El expression.

First, create a Java class elfuncutil, which contains static
The function is to return the string after the two parameters are added.

/*** Class description: ** @ author yjde */public class elfuncutil {/*** El method is used to connect two strings ** @ Param str1 * @ Param str2 * @ return */public static string append (string str1, string str2) {return str1 + str2 ;}}

Then write a TLD file elfunc. TLD.

<? 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 [url] Protocol" version = "2.0"> <tlib-version> 1.0 </tlib-version> <short-Name> elf </short-Name> <function> <description> used to connect two strings </description> <Name> append </Name> <function-class> COM. ourpalm. MIS. common. util. elfuncutil </function-class> <function-signature> JAVA. lang. string append (Java. lang. string, Java. lang. string) </function-signature> <example >$ {elf: append (str1, str2) }</example> </function> </taglib>

Finally, we can directly use our custom El expression in JSP. Do not forget to import TLD in JSP before use.

<%@ taglib prefix="elf" uri="/WEB-INF/tlds/elfunc.tld"%>
<c:out value="${elf:append(param.str1, param.str2)}"/>

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.