Java 怎么获取JSF的字段标签

2019年8月14日22:09:25 评论 28

1.简介

Java 怎么获取JSF的字段标签?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

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 = "tah1986.com";
        System.out.println(getMessageFromBundle(messageId));
    }
    
    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());

        

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

 

3.总结

示例代码演示了 Java 怎么获取JSF的字段标签,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: