如何在EL中使用静态方法? [英] How do I use static methods within EL?
问题描述
我正在使用jsp. bean.getConfigurationActionButtonBar()
返回按钮对象的列表. WebUtils.getActionButtonBar(List buttonList)
获取该列表并返回生成的html.很简单.
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>
按钮列表已设置.调用静态WebUtils.getActionButtonBar
时出了点问题.根本就不会打那个电话.有什么主意吗?
The button list is set. Something's wrong with the call to static WebUtils.getActionButtonBar
. That call is simply never made. Any idea?
推荐答案
您需要将其声明为EL函数,并将其注册在单独的taglib中.
You need to declare it as an EL function and register it in a separate taglib.
首先创建一个/WEB-INF/functions.tld
文件:
<?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())}
但是,您在实现具体功能要求方面走了完全错误的道路. HTML应该由JSP文件生成,而不是由原始Java代码生成.请改用JSP包含文件或标记文件.
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.
这篇关于如何在EL中使用静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!