如何在EL中引用常量? [英] How to reference constants in EL?
问题描述
如何在JSP页面上使用EL引用常量?
How do you reference an constants with EL on a JSP page?
我有一个接口地址
一个名为 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或更高版本
如果您已经使用Java 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" />
或添加一个EL上下文侦听器,该侦听器调用 ImportHandler#importClass()
如下:
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或更高版本中,不可能。有几种选择:
-
将它们放入
Map< String,Object>
放在应用程序范围内。在EL中,可以通过$ {map.key}
或$ {map ['key.with.dots']以通常的Javabean方式访问地图值。 }
。
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']}
.
使用< un:useConstants>
em> Unstandard 标签库(maven2回购此处):
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 }
。
使用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 }
。
如果您使用的是JSF2,则可以使用
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风格的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屋!