有没有一种简单的方法可以在 JSF 配置 .xml 文件(例如 faces-config.xml、web.xml 或 applicationContext.xml)中指定我想要从外部 .xml 文件检索配置的某些值。属性文件?这可以使用 JNDI 或类似的方法来完成吗?
例如:
<something>
#{brilliant-code-that-retrieves-values-from-.properties-file}
</something>
提前致谢。
最佳答案
是的,有;在 JSF 中,它是使用资源包完成的。
普通实现
从您的花园品种
.properties
文件开始currency.symbol = "$" currency.name = "dollar"
在您的faces.config.xml 文件中定义资源包
<resource-bundle> <base-name>com.you.resources.info</base-name> <var>paymentInfo</var> </resource-bundle>
在 faces-config.xml 中引用您的资源包(由上下文作为托管对象处理)。请记住,faces-config.xml 文件也处理 EL;出于所有意图和目的,您放置在其中的任何 EL 都会像在 View 中一样被扫描:
<managed-bean> <managed-bean-name>YourManagedBean</managed-bean-name> <managed-bean-class> com.you.app.BeanClass </managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>currencyName</property-name> <value>#{paymentInfo['currency.name']}</value> </managed-property> </managed-bean-name> </managed-bean>
加载在 spring 上下文中配置的属性文件
Spring 使用 util
命名空间提供开箱即用的属性文件加载 (xmlns:util="http://www.springframework.org/schema/util"
)。使用相同的配置文件:
在 applicationContext.xml 中定义配置器
<util:properties id="paymentInfo" location="classpath:com/you/resources/info/payment-info.properties" />
您可以像在 faces-config.xml 或 applicationContext.xml 中的#3 中一样使用 bean
#{ paymentInfo}
关于java - 如何从 .xml 配置文件 (JSF) 引用 Java .properties 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29340202/