Self-defined functions in EL expressions, el expressions

Source: Internet
Author: User
Tags tld

Self-defined functions in EL expressions, el expressions

Step 1:

Create a func. tld file under the WEB-INF/tld/directory as follows:

<Taglib xmlns = "http://java.sun.com/xml/ns/j2ee" version = "2.0"> <tlib-version> 1.0 </tlib-version> <short-name> func </short-name> <uri> http: /www.kevin.com/taglib/func </uri> <function> <name> testFunction </name> <function-class> com. lss. page. function. pageFunction </function-class> <! -- The Data Types of udfs must be exactly the same; otherwise, the corresponding methods cannot be found. For example, int and Integer are different --> <function-signature> String testFunction (int, long, java. lang. string) </function-signature> </function> </taglib>

Step 2:

The JAVA processing function code is as follows:

package com.lss.page.function;public class PageFunction {public static String testFunction(int num, long l, String str) {return num + "___" + l + "__" + str;}}
Step 3:
JSP uses this function

JSP header import:

<% @ Taglibprefix ="Func"Uri ="Http:/www.kevin.com/taglib/func"%>

<! -- This function can be used in JSP -->

$ {Func: testFunction (, 1, 'zhang Sanfeng ')}

Output 100010000_1 _ Zhang Sanfeng when accessing the JSP






Can java functions be used in EL expressions?

Yes, but you have to cheat the system.
The EL expression can only call get methods that contain attributes. Therefore, you can add a get prefix to the method you want to call, and delete get directly during the call.

Can el expressions be used in js functions? Solution list

Js is executed on the client, el is executed on the server, and the server is executed first than the client. Therefore, you cannot use js to change the value of the el expression that has been executed, however, el can be used to initialize and pass values to js. This is the same effect as jsp scripts. However, if the js script is not in the jsp page but in a separate js file, in this case, using el for initial value transfer does not work, but you can use window. onload () to get the el value and then send it to the method in the js file for variable Initialization

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.