Java JSF 2 - 如何使用 JSF EL 从 web.xml 获取上下文参数值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6523193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:37:14  来源:igfitidea点击:

JSF 2 - How can I get a context-param value from web.xml using JSF EL?

javajsf-2

提问by Jim Tough

I would like to declare some constant values used by my JSF 2 webapp inside the web.xml file like so:

我想在 web.xml 文件中声明我的 JSF 2 webapp 使用的一些常量值,如下所示:

<web-app>
    <context-param>
        <param-name>myconstantkey</param-name>
        <param-value>some string value</param-value>
    </context-param>
</web-app>

Getting these values from inside a backing bean is easy enough:

从支持 bean 内部获取这些值很容易:

FacesContext ctx = FacesContext.getCurrentInstance();
String myConstantValue =
    ctx.getExternalContext().getInitParameter("myconstantkey");

How do I achieve the same thing from inside a Facelets page using JSF EL to get the value?

如何使用 JSF EL 从 Facelets 页面内部实现同样的事情来获取值?

采纳答案by Jim Tough

Steve Taylor's answer does indeed work, but there is a simpler way using the JSF EL pre-defined object initParam.

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

最初这对我不起作用,因为我忘记在键名周围加上单引号并且返回一个空字符串。此解决方案还应适用于包含点字符的键值。

回答by Distortum

#{facesContext.externalContext.initParameterMap.myconstantkey}

回答by Pramod Jain

Through EL

通过EL

${initParam['myconstantkey']}