Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read Properties file Dynamically in JSF

 
rakesh reddy A P
Greenhorn
Posts: 2
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is your question? I don't see one?
 
Brendan Healey
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17616
38
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not post the same question more than once. It confuses people. I'm going to lock the other copy.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic