如何创建自定义 EL 函数来调用静态方法? [英] How to create a custom EL function to invoke a static method?
问题描述
我是 JSF 2 的新手.我的问题与 BalusC 对此问题的回答有关 基于请求参数的 jsf2 ajax 更新部分 我尝试了 BalusC 发布的 kickstart 代码,但遇到了 EL 解析错误:
Im new to JSF 2. My question is related to BalusC's answer to this question jsf2 ajax update parts based on request parameters I tried the kickstart code BalusC posted and I encountered an EL parsing error:
/nameofpage.xhtml @12,64 rendered="#{bean.panels.contains('u1')}"
Error Parsing: #{bean.panels.contains('u1')}
我猜这是因为我没有运行支持 Servlet 3.0/EL 2.2 的容器,其中/WEB-INF/web.xml 根据 Servlet 3.0 规范声明.我使用的是 Tomcat 6.
I guess that this is caused because I'm not running a Servlet 3.0 / EL 2.2 capable container with a /WEB-INF/web.xml declared as per Servlet 3.0 spec. I'm using Tomcat 6.
BalusC 在他的回答中建议创建自定义 EL 函数.但是如何使用自定义 EL 函数来完成此操作?或者可以通过配置我的项目的某些部分来解决这个问题?
BalusC suggested in his answer to create a custom EL function. But how do I accomplish this using a custom EL function? Or can this be fixed by just configuring certain parts of my project?
下面是我的web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
推荐答案
首先使用 public static
方法创建一个 final
类,它完全可以完成您想要的工作:
First create a final
class with a public static
method which does exactly the job you want:
package com.example;
import java.util.Collection;
public final class Functions {
private Functions() {
// Hide constructor.
}
public static boolean contains(Collection<Object> collection, Object item) {
return collection.contains(item);
}
}
然后在/WEB-INF/functions.taglib.xml
中定义为facelet-taglib
:
<?xml version="1.0" encoding="UTF-8"?>
<facelet-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-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://example.com/functions</namespace>
<function>
<function-name>contains</function-name>
<function-class>com.example.Functions</function-class>
<function-signature>boolean contains(java.util.Collection, java.lang.Object)</function-signature>
</function>
</facelet-taglib>
然后使用现有/WEB-INF/web.xml
中的新标签库熟悉Facelets:
Then familarize Facelets with the new taglib in the existing /WEB-INF/web.xml
:
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/functions.taglib.xml</param-value>
</context-param>
(注意:如果你已经定义了javax.faces.FACELETS_LIBRARIES
,那么你可以添加新的路径分号分隔)
(note: if you already have the javax.faces.FACELETS_LIBRARIES
definied, then you can just add the new path semicolon separated)
然后在 Facelets XHTML 文件中将其定义为新的 XML 命名空间:
Then define it in the Facelets XHTML file as new XML namespace:
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:func="http://example.com/functions"
...
>
终于可以按预期使用它了:
Finally you can use it as intended:
rendered="#{func:contains(bean.panels, 'u1')}"
<小时>
作为一个完全不同的选择,您还可以在您的项目中包含 JBoss EL.它适用于 Tomcat 6.0,您将能够在 EL 中调用非 getter 方法.删除 jboss-el.jar 文件在 /WEB-INF/lib
并将以下内容添加到您的 web.xml
中:
As a completely different alternative, you can also include JBoss EL in your project. It works on Tomcat 6.0 and you'll be able to invoke non-getter methods in EL. Drop jboss-el.jar file in /WEB-INF/lib
and add the following to your web.xml
:
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>
<小时>
从 EL 2.2 开始,还有另一种方法:创建一个 @ApplicationScoped
bean,其中的方法依次引用那些静态函数.另见 a.o.应用程序作用域 bean 中的实用方法.
Since EL 2.2 there's another approach: create an @ApplicationScoped
bean with methods in turn referring to those static functions. See also a.o. Utility methods in application scoped bean.
这篇关于如何创建自定义 EL 函数来调用静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!