Invoking Java static methods in El

Source: Internet
Author: User
Tags tld

You recently encountered a problem in your project that calls for static methods, such as:

<items=var=<p> total: ${com.example.tools.gettotal (bean.nums)}</p> </c:foreach>       

However, the above code can not be compiled, can only seek other methods. After reviewing various documents, we found 3 kinds of solutions.

1, create a Get method directly for the bean

Double Gettotal () {       return com.example.Tools.getTotal (nums);}

Then use it directly in El:

Total:${bean.total}

2, create the tools#gettotal as an El function. First create a /WEB-INF/my.tld file:

<?XML version= "1.0" encoding= "UTF-8"?><Taglib xmlns="Http://java.sun.com/xml/ns/javaee" Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1" > <Display-name>custom Functions</Display-name> <tlib-version>1.0</Tlib-version><Short-name>my</Short-name><!--short-name elements don't write-- <Uri>http://example.com/functions</Uri> <Function>  <name>calculatetotal</name>  <function-class>com.example.tools</< Span class= "title" >function-class>  < function-signature>double gettotal (double[]) </< span class= "tag" >function-signature> </function></ Taglib>               

Then define the mappings for URI and TLD file paths in Web. xml:

<jsp-config>        <<taglib-uri>http://example.com/functions</  <taglib-location>/web-inf/my.tld</</</jsp-config>   

The taglib is then introduced into the JSP header to be used:

<%@ taglib uri="http://example.com/functions" prefix="%>     

Where the URI corresponds to the Taglib-uri in Web. Xml. Finally, the function can be used in El:

<items=var=<p> total: ${my:calculatetotal (bean.nums)}</p>  </c:foreach>       

3, use spring's spel:

JSP Header Introduction:

<%@taglib prefix="S" uri="%>     

Use:

<items=var=<expression=var=<p> total: ${total}</p ></c:foreach>          

Invoking Java static methods in El

Related Article

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.