Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从.xml配置文件(JSF)中引用Java.properties文件?_Java_Xml_Spring_Jsf_Jsf 2 - Fatal编程技术网

如何从.xml配置文件(JSF)中引用Java.properties文件?

如何从.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

有没有一种简单的方法可以在JSF configuration.xml文件(例如,faces-config.xml、web.xml或applicationContext.xml)中指定我要从外部.properties文件检索配置的某些值?这是使用JNDI或类似工具可以实现的吗

例如:

<something>
#{brilliant-code-that-retrieves-values-from-.properties-file}
</something>

#{从-.properties文件检索值的精彩代码}

提前谢谢。

是的,有;在JSF中,它是使用资源包完成的

常规实施

  • 从您的花园品种
    .properties
    文件开始

    currency.symbol = "$"
    currency.name = "dollar"
    
  • 在faces.config.xml文件中定义资源包

  • 您可以在faces-config.xml或applicationContext.xml中使用bean
    {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;
    }