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?
推荐答案
史蒂夫泰勒的答案确实有效,但有一种更简单的方法使用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屋!