How to Call a Static Method in Jsp/El

How to call a static method in JSP/EL?

You cannot invoke static methods directly in EL. EL will only invoke instance methods.

As to your failing scriptlet attempt, you cannot mix scriptlets and EL. Use the one or the other. Since scriptlets are discouraged over a decade, you should stick to an EL-only solution.

You have basically 2 options (assuming both balance and Calculate#getAmount() are double).

  1. Just wrap it in an instance method.

    public double getAmount() {
    return Calculate.getAmount(balance);
    }

    And use it instead:

    Amount: ${row.amount}

  2. Or, declare Calculate#getAmount() as an EL function. First create a /WEB-INF/functions.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>
    <uri>http://example.com/functions</uri>

    <function>
    <name>calculateAmount</name>
    <function-class>com.example.Calculate</function-class>
    <function-signature>double getAmount(double)</function-signature>
    </function>
    </taglib>

    And use it as follows:

    <%@taglib uri="http://example.com/functions" prefix="f" %>
    ...
    Amount: ${f:calculateAmount(row.balance)}">

How do I use static methods within EL?

You need to declare it as an EL function and register it in a separate taglib.

First create a /WEB-INF/functions.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>
<uri>http://example.com/util</uri>

<function>
<name>getActionButtonBar</name>
<function-class>com.example.WebUtils</function-class>
<function-signature>java.lang.String getActionButtonBar(java.util.List)</function-signature>
</function>
</taglib>

Then you can use it as follows:

<%@taglib uri="http://example.com/util" prefix="util" %>
...
${util:getActionButtonBar(bean.getConfigurationActionButtonBar())}

However, you're going completely the wrong path as to achieving the concrete functional requirement. HTML should be generated by JSP files, not by raw Java code. Use a JSP include file or tag file instead.

Issue while calling a static Method in jsp

Make few change as mentioned below and check it again.

  • Define <uri>SubstrDescriptor</uri> in tld.
  • Use <%@ taglib prefix="test" uri="SubstrDescriptor"%> in JSP.

Please have a look at similar post How to call a static method in JSP/EL? that might help you for better understanding.

JSP:

<%@ taglib prefix="test" uri="SubstrDescriptor"%>
<body>
${test:concat("java")}
</body>

SubstrDescription.tld:

<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/web-jsptaglibrary_2_0.xsd"
version="2.0">

<tlib-version>2.0</tlib-version>
<uri>SubstrDescriptor</uri>
<function>
<name>concat</name>
<function-class>java4s.Demo</function-class>
<function-signature>java.lang.String doMyStuff( java.lang.String )
</function-signature>
</function>
</taglib>

Project structure:

WebContent
|
|__WEB-INF
| |
| |__SubstrDescription.tld
| |__web.xml
|
|__index.jsp

Access static property or method in JSP via EL 3.0 (JEE7; Tomcat 8)

UPDATE:

There is a bug in Tomcat's (at least as of 8.0.9) jsp-api.jar. According to the change log, it is fixed in Tomcat version 8.0.15.

As a workaround, in the apache-tomcat-8.0.9\lib folder replace jsp-api.jar with javax.servlet.jsp-api-2.3.2-b01.jar. Refresh the project in eclipse and you will see the output for

     Testing: ${Boolean.TRUE}

as:

    Testing: true

This was identified as a bug in GLASSFISH as well here.

In order to access static fields or methods outside of the java.lang package, those specific packages or classes must be added to the EL context (also discussed by BalusC here).

Here's an example allowing static access to classes in the java.time package for all jsp files in your web application:

@WebListener
public class Config implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
JspFactory.getDefaultFactory().getJspApplicationContext(event.getServletContext()).addELContextListener((ELContextEvent e) -> {
e.getELContext().getImportHandler().importPackage("java.time");
});
}

@Override
public void contextDestroyed(ServletContextEvent event) {}
}

And now from the jsp, to return the current LocalDate, for example:

${LocalDate.now()}

Note that ${java.time.LocalDate.now()} does not work.

How can I access java static field in jsp page use EL expression?

You have to save this object fe. in session scope:

setAttribute("FIELD", Constans.FIELD)

you can set this in servlet and call EL expression in jsp page:

${FIELD}

You can add to session or request only object. Not a class.
https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpSession.html#setAttribute(java.lang.String, java.lang.Object)

How to get static method value in jsp without creation bean in Spring?

how about this?

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<spring:eval expression="T(com.abc.xyz.YourClassName).bytesToKB(2560)" var="result"></spring:eval>
Spring Eval result: <c:out value="${result }"/>


Related Topics



Leave a reply



Submit