aspose file tools*
The moose likes JSF and the fly likes How to read Properties file Dynamically in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to read Properties file Dynamically in JSF" Watch "How to read Properties file Dynamically in JSF" New topic
Author

How to read Properties file Dynamically in JSF

rakesh reddy A P
Greenhorn

Joined: Aug 14, 2012
Posts: 2

We are trying to create Localization Example App In that according to user selection language has to change. I am giving my code here can any one help me in this

Language selection page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Select Language"></h:outputText>
<h:selectOneMenu id="dropdown" value="#{languageDetails.locale1}">
<f:selectItem itemValue="en" itemLabel="English" />
<f:selectItem itemValue="es" itemLabel="Spanish" />
<f:selectItem itemValue="fr" itemLabel="French" />
</h:selectOneMenu>
</h:panelGrid>
<p><h:commandButton id="change" value="Change Language"
action="#{languageDetails.changeLanguage}" /></p>
</h:form>

</f:view>
</body>
</html>


Here in this page i need to get language according to user selection

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view locale="#{languageDetails.locale2}">

<h:form>
<table>
<f:loadBundle var="msg" basename="com.bean.messages_en"/>

<h:panelGrid columns="2">
<tr><td><h:outputText value="#{msg.name}"></h:outputText></td>
<td><h:inputText value="#{userDetails.name}"></h:inputText></td></tr>
<tr><td><h:outputText value="#{msg.age}"></h:outputText></td>
<td><h:inputText value="#{userDetails.age}"></h:inputText></td></tr>
<tr><td><h:outputText value="#{msg.mail}"></h:outputText></td>
<td><h:inputText value="#{userDetails.email}"></h:inputText></td></tr>
<tr><td><h:outputText value="#{msg.dob}"></h:outputText></td>
<td><h:inputText id="dob1" value="#{userDetails.dob}">
<f:convertDateTime type="date" pattern="MM/dd/yyyy" />
<h:message for="dob1"></h:message>
</h:inputText></td></tr>
<tr><td><h:commandButton value="submit" action="submitted"/></td></tr>
</h:panelGrid>
</table>
</h:form>

</f:view>
</body>
</html>


faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
<managed-bean>
<managed-bean-name>languageDetails</managed-bean-name>
<managed-bean-class>com.bean.Language</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>


<navigation-rule>
<from-view-id>/Input.jsp</from-view-id>
<navigation-case>
<from-outcome>changed</from-outcome>
<to-view-id>/UserDetailsForm.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>es</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<message-bundle>com.bean.messages_en</message-bundle>
<message-bundle>com.bean.messages_es</message-bundle>
<message-bundle>com.bean.messages_fr</message-bundle>
</application>
</faces-config>



We are using three propertie files. According to user selection particular propertie file has to load

Please can anyone help in this. Thanks in Advance.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1021
    
    5

What exactly is your question? I don't see one?
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
Hazarding a guess, you need to look at f:view locale="#{bean.language}" where the language property is
something like "en", "es" or "fr" and you have files like messages_fr.properties and these are supported-locale's
in faces-config.

The bean should be session scoped and you also need to change the locale in the view when the user selects
a new language:



Also it's recommended to use h:selectOneMenu onchange="submit()" to synchronously refresh the page.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

Please do not post the same question more than once. It confuses people. I'm going to lock the other copy.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read Properties file Dynamically in JSF