How do I use static methods within EL?
Asked Answered
C

1

0

I'm working in a jsp. bean.getConfigurationActionButtonBar() returns a list of button objects. WebUtils.getActionButtonBar(List buttonList) takes that list and returns generated html. Very simple.

Now, for some reason this doesn't work:

<td colspan="2">
    ${WebUtils.getActionButtonBar(bean.getConfigurationActionButtonBar())}
</td>

The button list is set. Something's wrong with the call to static WebUtils.getActionButtonBar. That call is simply never made. Any idea?

Cover answered 23/3, 2012 at 11:52 Comment(0)
O
3

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.

Officer answered 23/3, 2012 at 11:56 Comment(2)
Well, I'm not going the wrong path. This was a call to a static method that assembles html into a StringBuilder and spits it out. Typical custom tag logic. I simply wanted to see what I'm going to get even before I start adding this to custom tag support. I was baffled, as to why jstl couldn't get to it. Your answer tells me jstl doesn't know anything about WebUtils class or its methods. I haven't tried your function setup because I prefer to stick to my custom tag tld and have already moved this code there. Thanks for your contribution.Cover
Only Java could forbid the intuitive 3-line solution in favor of a redundant 17-line definition and call it "progress"Hartmunn

© 2022 - 2024 — McMap. All rights reserved.