如何在 EL 中引用常量? [英] How to reference constants in EL?

查看:16
本文介绍了如何在 EL 中引用常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 JSP 页面上使用 EL 引用常量?

How do you reference an constants with EL on a JSP page?

我有一个接口 Addresses,其中有一个名为 URL 的常量.我知道我可以通过以下方式使用脚本引用它:<%=Addresses.URL%>,但是我如何使用 EL 来执行此操作?

I have an interface Addresses with a constant named URL. I know I can reference it with a scriplet by going: <%=Addresses.URL%>, but how do I do this using EL?

推荐答案

EL 3.0 或更新版本

如果您已经在 J​​ava EE 7/EL 3.0 上,那么 @page import 也会在 EL 范围内导入类常量.

EL 3.0 or newer

If you're already on Java EE 7 / EL 3.0, then the @page import will also import class constants in EL scope.

<%@ page import="com.example.YourConstants" %>

这将在幕后通过 ImportHandler#importClass() 并作为 ${YourConstants.FOO} 提供.

This will under the covers be imported via ImportHandler#importClass() and be available as ${YourConstants.FOO}.

请注意,所有 java.lang.* 类都已经隐式导入并可用,例如 ${Boolean.TRUE}${Integer.MAX_VALUE}.这只需要更新的 Java EE 7 容器服务器,因为早期版本在这方面存在错误.例如.GlassFish 4.0 和 Tomcat 8.0.0-1x 失败,但 GlassFish 4.1+ 和 Tomcat 8.0.2x+ 有效.并且您需要绝对确保您的 web.xml 声明符合服务器支持的最新 servlet 版本.因此,对于声明符合 Servlet 2.5 或更早版本的 web.xml,Servlet 3.0+ 的所有功能都将无法使用.

Note that all java.lang.* classes are already implicitly imported and available like so ${Boolean.TRUE} and ${Integer.MAX_VALUE}. This only requires a more recent Java EE 7 container server as early versions had bugs in this. E.g. GlassFish 4.0 and Tomcat 8.0.0-1x fails, but GlassFish 4.1+ and Tomcat 8.0.2x+ works. And you need to make absolutely sure that your web.xml is declared conform the latest servlet version supported by the server. Thus with a web.xml which is declared conform Servlet 2.5 or older, none of the Servlet 3.0+ features will work.

另请注意,此功能仅在 JSP 中可用,而在 Facelets 中不可用.对于 JSF+Facelets,最好的选择是使用 OmniFaces <o:importConstants>如下:

Also note that this facility is only available in JSP and not in Facelets. In case of JSF+Facelets, your best bet is using OmniFaces <o:importConstants> as below:

<o:importConstants type="com.example.YourConstants" />

或者添加一个调用 ImportHandler#importClass() 的 EL 上下文监听器,如下所示:

Or adding an EL context listener which calls ImportHandler#importClass() as below:

@ManagedBean(eager=true)
@ApplicationScoped
public class Config {

    @PostConstruct
    public void init() {
        FacesContext.getCurrentInstance().getApplication().addELContextListener(new ELContextListener() {
            @Override
            public void contextCreated(ELContextEvent event) {
                event.getELContext().getImportHandler().importClass("com.example.YourConstants");
            }
        });
    }

}

<小时>

EL 2.2 或更早版本

这在 EL 2.2 及更早版本中不可能.有几种选择:

  1. 将它们放入一个 Map 中,然后将其放入应用程序范围.在 EL 中,可以通过 ${map.key}${map['key.with.dots']} 以通常的 Javabean 方式访问地图值.

  1. Put them in a Map<String, Object> which you put in the application scope. In EL, map values are accessible the usual Javabean way by ${map.key} or ${map['key.with.dots']}.

使用非标准标签库的(maven2 repo 此处):

Use <un:useConstants> of the Unstandard taglib (maven2 repo here):

<%@ taglib uri="http://jakarta.apache.org/taglibs/unstandard-1.0" prefix="un" %>
<un:useConstants className="com.example.YourConstants" var="constants" />

通过这种方式,它们可以通过 ${constants.FOO} 以通常的 Javabean 方式访问.

This way they are accessible the usual Javabean way by ${constants.FOO}.

使用 Javaranch 的 CCC <ccc:constantsMap>这篇文章的底部有描述.

Use Javaranch's CCC <ccc:constantsMap> as desribed somewhere at the bottom of this article.

<%@ taglib uri="http://bibeault.org/tld/ccc" prefix="ccc" %>
<ccc:constantsMap className="com.example.YourConstants" var="constants" />

这样,它们也可以通过 ${constants.FOO} 以通常的 Javabean 方式访问.

This way they are accessible the usual Javabean way by ${constants.FOO} as well.

如果您使用的是 JSF2,那么您可以使用 <o:OmniFaces 的 importConstants>.

If you're using JSF2, then you could use <o:importConstants> of OmniFaces.

<html ... xmlns:o="http://omnifaces.org/ui">
<o:importConstants type="com.example.YourConstants" />

通过这种方式,#{YourConstants.FOO} 也可以通过通常的 Javabean 方式访问它们.

This way they are accessible the usual Javabean way by #{YourConstants.FOO} as well.

创建一个包装类,通过 Javabean 风格的 getter 方法返回它们.

Create a wrapper class which returns them through Javabean-style getter methods.

创建一个自定义 EL 解析器,它首先扫描常量是否存在,如果不存在,则委托给默认解析器,否则返回常量值.

Create a custom EL resolver which first scans the presence of a constant and if absent, then delegate to the default resolver, otherwise returns the constant value instead.

这篇关于如何在 EL 中引用常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆