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