JSF 2 - 如何使用 JSF EL 从 web.xml 获取上下文参数值? [英] JSF 2 - How can I get a context-param value from web.xml using JSF EL?
问题描述
我想在 web.xml 文件中声明我的 JSF 2 webapp 使用的一些常量值,如下所示:
I would like to declare some constant values used by my JSF 2 webapp inside the web.xml file like so:
<web-app>
<context-param>
<param-name>myconstantkey</param-name>
<param-value>some string value</param-value>
</context-param>
</web-app>
从支持 bean 内部获取这些值很容易:
Getting these values from inside a backing bean is easy enough:
FacesContext ctx = FacesContext.getCurrentInstance();
String myConstantValue =
ctx.getExternalContext().getInitParameter("myconstantkey");
如何使用 JSF EL 从 Facelets 页面内部获得相同的值?
How do I achieve the same thing from inside a Facelets page using JSF EL to get the value?
推荐答案
Steve Taylor 的回答确实有效,但有一种更简单的方法使用 JSF EL 预定义对象 initParam
.
Steve Taylor's answer does indeed work, but there is a simpler way using the JSF EL pre-defined object initParam
.
<h:outputText value="#{initParam['myconstantkey']}" />
最初这对我不起作用,因为我忘记在键名周围加上单引号并且返回一个空字符串.此解决方案也适用于包含点字符的键值.
Originally this wasn't working for me because I forgot to put the single quotes around the key name and was getting back an empty string. This solution should also work with key values that contain dot characters.
这篇关于JSF 2 - 如何使用 JSF EL 从 web.xml 获取上下文参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!