Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

jsf - fetching the values from a property file inside a XHTML file

I have a properties file in my local File system. I have used a XHTML file to create a UI. In this file I have hardcoded the names for UI elements.Is there any way to fetch those names from the properties file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a couple of options, assume you have a messages.properties in the following location

src
   main
      java
      resources 
         com
            example
               messages.properties

You can use the resource-bundle tag in the faces-config.xml to make it application wide

<application>
   <locale-config>
        <default-locale>en</default-locale>
        <supported-locale>fr</supported-locale>
    </locale-config>
   <resource-bundle>
      <baseName>com.example.messages</baseName>
      <var>msgs</var>
   </resource-bundle>
</application>

Then it can be accessed in your XHTML like so (note the use of locale gives you the option of having a messages_fr.properties)

<f:view locale="en">
   #{msgs.keyName}
</f:view>
<f:view locale="fr">
   #{msgs.keyName}
</f:view>

You can also use <f:loadBundle>

<f:view locale="en">
   <f:loadBundle baseName="com.example.messages" var="msgs" />
   #{msgs.keyName}
</f:view>
<f:view locale="fr">
   <f:loadBundle baseName="com.example.messages" var="msgs" />
   #{msgs.keyName}
</f:view>

If you need the Resource bundle to be external one way might be the following approach.

Set the absolute path to your bundle in your web.xml

<context-param>
    <param-name>bundlePath</param-name>
    <param-value>absolute path to your properties file</param-value>
</context-param>

Create your own ResourceBundle class

package com.example;

import java.io.FileReader;

import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.PropertyResourceBundle;

import javax.faces.context.FacesContext;

public class MyBundle extends ResourceBundle {

    public MyBundle() throws FileNotFoundException {
        String configPath = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("bundlePath")
        setParent(new PropertyResourceBundle(new FileReader(configPath)));
    }

    @Override
    public Object handleGetObject(String key) {
        return parent.getObject(key);
    }

    @Override
    public Enumeration<String> getKeys() {
        return parent.getKeys();
    }

}

Register it in the faces-config.xml as per usual

<resource-bundle>
    <bundleName>com.example.MyBundle</bundleName>
    <var>msgs</var>
</resource-bundle>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...