Hello
I have a couple classes here and a resource bundles and a JSF that sends the key to the class (MessageProvider) and MessageManager.java gets the resource for the selectOneMenu component label.
I would like to change the label on the submit button depending on the choice from the selectOneMenu.
MusicLilbrary.ui.appBundle:
# To change this template, choose Tools | Templates
# and open the template in the editor.
title=What Type of Search do you desire?
formSubmit=New Search
formSubmit_artist=Search for a particular artist.
formSubmit_song=Search for a particular Song
Could someone look at my code and explain some of the many reasons this is not working? I am having trouble getting it to access the resource bundle.
Populated by an array choices , itemselected to searchCategory
<p>
What type of search do you desire?
<h:selectOneMenu label="#{msgMgr.searchCategory}"required="true">
<f:selectItems value="#{SearchContent.choices}" id="choiceMenu"/>"
</h:selectOneMenu>
<h:commandButton id="newSearch" value="#{msg.formSubmit}" action="submit"/>
</p>
<application>
<resource-bundle>
<base-name>MusicLibrary.ui.appBundle</base-name>
<var>msgbundle</var>
</resource-bundle>
<resource-bundle>
<base-name>MusicLibrary.ui.appBundleJunior</base-name>
<var>juniormsgbundle</var>
</resource-bundle>
<resource-bundle>
<base-name>MusicLibrary.ui.appBundleSenior</base-name>
<var>seniorrmsgbundle</var>
</resource-bundle>
</application>
<managed-bean>
<description>context sensitive resource processing-intercepting the ResourceBundle request_intercept</description>
<managed-bean-name>msgMgr</managed-bean-name>
<managed-bean-class>csrp.MessageManager</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>context sensitive resource processing-intercepting the ResourceBundle request_implement interception</description>
<managed-bean-name>msg</managed-bean-name>
<managed-bean-class>csrp.MessageProvider</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>msgMgr</property-name>
<value>#{msgMgr}</value>
</managed-property>
</managed-bean>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package MusicLibrary.csrp;
/**
*
* @author depot
*/
import java.util.HashMap;
public class MessageProvider extends HashMap {
public MessageManager msgMgr;
public MessageProvider() {
}
@Override
public Object get(Object key) {
return msgMgr.getMessage((String) key);
}
public void setMsgMgr(MessageManager msgMgr) {
this.msgMgr = msgMgr;
}
public MessageManager getMsgMgr() {
return msgMgr;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package MusicLibrary.csrp;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
public class MessageManager {
public String getMessage(String key) {
// use standard JSF Resource Bundle mechanism
return getMessageFromJSFBundle(key);
// use the default Java ResourceBund;e mechanism
// return getMessageFromResourceBundle(key);
}
private String getMessageFromResourceBundle(String key) {
ResourceBundle bundle = null;
String bundleName =
"MusicLibrary.ui.appBundle";
String message = "";
Locale locale =
FacesContext.getCurrentInstance().getViewRoot().getLocale();
try {
bundle =
ResourceBundle.getBundle(bundleName, locale, getCurrentLoader(bundleName));
} catch (MissingResourceException e) {
// bundle with this name not found;
}
if (bundle == null)
return null;
try {
message = bundle.getString(key);
} catch (Exception e) {
}
return message;
}
private String getMessageFromJSFBundle(String key) {
return (String)resolveExpression("#{msgbundle['" + key + "']}");
}
public static ClassLoader getCurrentLoader(Object fallbackClass) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null)
loader = fallbackClass.getClass().getClassLoader();
return loader;
}
// from JSFUtils in Oracle ADF 11g Storefront Demo
public static Object resolveExpression(String expression) {
FacesContext facesContext = FacesContext.getCurrentInstance();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExp =
elFactory.createValueExpression(elContext, expression,
Object.class);
return valueExp.getValue(elContext);
}
}
Thanks
-ceyesuma