How do I use static methods within EL?

时光总嘲笑我的痴心妄想 提交于 2019-12-12 00:52:41

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9838710/how-do-i-use-static-methods-within-el

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!