ceyesuma -4 Posting Pro


Name:

The name of this question is bundled_resources Hello and Thank you in advance for any assistance.

Purpose:



The purpose of this code is to change the label of a commandButton by hijacking the resource bundle functionality and setting the label name relative to the selection of the selectOneMenu



Question:



My question concerning this code is why there is navigation to the page containing the commandButton but no label on the button. Further, when the button is clicked I get an error: see error:



Functionality:



The actual functionality of this code is to obtain the selection and override the hashmap, concatenate paths and bundle names, return keys value from proper bundle.



errors:



The errors related to this code are:
package pages.content;
ResourceBundles are immutable



Description



Code description: populated by choices[],searchCategory var for itemselected
Note the label on the button.

<p>
                    What type of search do you desire?
                    <h:selectOneMenu value="#{msg.searchCategory}"required="true">
                        <f:selectItems value="#{SearchContent.choices}" id="choiceMenu"/>"
                    </h:selectOneMenu>
                    <h:commandButton id="newSearch" label="#{msgMgr.formSubmit}" type="submit" action="#{content.listSetup}"/>
  </p>



Description



Code description:resource bundles(xml)

<application>
        <resource-bundle>
            <base-name>MusicLibrary.ui.appBundle</base-name>
            <var>msgbundle</var>
        </resource-bundle>
        <resource-bundle>
            <base-name>MusicLibrary.ui.appBundleArtist</base-name>
            <var>artistmsgbundle</var>
        </resource-bundle>
        <resource-bundle>
            <base-name>MusicLibrary.ui.appBundleSong</base-name>
            <var>songmsgbundle</var>
        </resource-bundle>
    </application>



Description



Code description:xml,managed beans Note the managed property

<managed-bean>
        <description>context sensitive resource processing-intercepting the ResourceBundle request_intercept</description>
        <managed-bean-name>msgMgr</managed-bean-name>
        <managed-bean-class>MusicLibrary.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>MusicLibrary.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>



Description



Code description:MessageProvider

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;
    }
}



Description



Code description:MessageManager.java

* 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;

/**
 *
 * @author depot
 */
public class MessageManager {

    private String searchCategory;

    public String getMessage(String key) {
          String msg = getMessageFromPrefixedBundle(key, (String) searchCategory);
        if (msg == null || msg.startsWith("???")) {
            msg = getMessageFromJSFBundle(key);
        }
        return msg;
    }

    private String getMessageFromResourceBundle(String key, String bundlePostfix) {
        ResourceBundle bundle = null;
        String bundleName = "MusicLibrary.ui.appBundle" + (bundlePostfix == null ? "" : bundlePostfix);
        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 getMessageFromPrefixedBundle(String key, String bundlePrefix) {
        return (String) resolveExpression("#{" + bundlePrefix + "msgbundle['" + key + "']}");
    }

    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 again.
-ceyesuma




Solution:



The solutions related to this code are

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.