如何从.xml配置文件(JSF)中引用Java.properties文件?
有没有一种简单的方法可以在JSF configuration.xml文件(例如,faces-config.xml、web.xml或applicationContext.xml)中指定我要从外部.properties文件检索配置的某些值?这是使用JNDI或类似工具可以实现的吗 例如:如何从.xml配置文件(JSF)中引用Java.properties文件?,java,xml,spring,jsf,jsf-2,Java,Xml,Spring,Jsf,Jsf 2,有没有一种简单的方法可以在JSF configuration.xml文件(例如,faces-config.xml、web.xml或applicationContext.xml)中指定我要从外部.properties文件检索配置的某些值?这是使用JNDI或类似工具可以实现的吗 例如: <something> #{brilliant-code-that-retrieves-values-from-.properties-file} </something> #{从-.pr
<something>
#{brilliant-code-that-retrieves-values-from-.properties-file}
</something>
#{从-.properties文件检索值的精彩代码}
提前谢谢。是的,有;在JSF中,它是使用资源包完成的 常规实施
.properties
文件开始
currency.symbol = "$"
currency.name = "dollar"
{paymentInfo}
,就像在上面的#3中一样 <util:properties id="paymentInfo" location="classpath:com/you/resources/info/payment-info.properties" />
对于web.xml: 只需在其中放入一个上下文参数
<context-param>
<param-name>something</param-name>
<param-value>#{brilliant-code-that-retrieves-values-from-.properties-file}</param-value>
</context-param>
OP询问如何在配置XML文件中访问它,而不是如何在JSF视图中访问它。啊,现在我看到了@baluscsho谢,但我如何检索.properties文件值并在XML配置文件中使用它?我认为您的示例检索一个上下文参数值,并在.java代码中使用它。
<util:properties id="paymentInfo" location="classpath:com/you/resources/info/payment-info.properties" />
<context-param>
<param-name>something</param-name>
<param-value>#{brilliant-code-that-retrieves-values-from-.properties-file}</param-value>
</context-param>
@Inject
private ServletContext context;
public MySomething getMySomething() {
final String mySomethingString = context.getInitParameter("something");
final ELContext elCtx = FacesContext.getCurrentInstance().getELContext();
(MySomething) mySomething = (MySomething) child.getValueExpression().getValue(elCtx);
return mySomething;
}