Get the field label for JSF. - Java JSF

Java examples for JSF:UIComponent

Description

Get the field label for JSF.

Demo Code

/*//  w w w  . j a  v  a  2 s .c o m
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

public class Main{
    public static void main(String[] argv) throws Exception{
        String messageId = "java2s.com";
        System.out.println(getMessageFromBundle(messageId));
    }
    /**
     * Get the field label.
     * 
     * @param messageId
     *            id of message in the resourcebundle
     * @return Message from the Message Source.
     */
    public static String getMessageFromBundle(final String messageId) {
        FacesContext context = FacesContext.getCurrentInstance();
        Locale locale = context.getViewRoot().getLocale();

        String bundleName = context.getApplication().getMessageBundle();

        ResourceBundle bundle = ResourceBundle.getBundle(bundleName,
                locale, getClassLoader());

        /** Look for formId.fieldName, e.g., EmployeeForm.firstName. */

        String label = null;
        try {
            label = bundle.getString(messageId);
            return label;
        } catch (MissingResourceException e) {
        }

        return label;
    }
    private static ClassLoader getClassLoader() {
        ClassLoader classLoader = Thread.currentThread()
                .getContextClassLoader();
        if (classLoader == null) {
            return FacesComponentUtility.class.getClassLoader();
        }
        return classLoader;
    }
}

Related Tutorials