Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...