📄 messages.java
字号:
package com.cim.jsf.util;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
public class Messages {
/**
* Gets the resource bundle associated with current context
* @param context FacesContext
* @return ResourceBundle
*/
public static ResourceBundle getBundle(FacesContext context) {
Application app = context.getApplication();
String appBundle = app.getMessageBundle();
Locale locale = getLocale( context );
ClassLoader loader = getClassLoader();
ResourceBundle bundle = null;
//System.out.println( "appBundle::" + appBundle);
if (appBundle != null) {
bundle = ResourceBundle.getBundle( appBundle, locale, loader );
}
return bundle;
}
/**
* Gets the localized string from the resource bundles
* Returns null if resource not found in the resource bundle.
* @param bundle ResourceBundle
* @param resourceId String
* @return String
*/
public static String getMessageResourceString(ResourceBundle bundle, String resourceId) {
String resource = null;
if ( bundle != null ) {
try {
//System.out.println("resourceId::" + resourceId);
resource = bundle.getString( resourceId );
//System.out.println("resource::" + resource);
} catch ( MissingResourceException ex ) {
return null;
}
}
if (resource == null) return null; // no match
else return resource;
}
public static FacesMessage getMessage(String bundleName, String resourceId,
Object[] params) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
String appBundle = app.getMessageBundle();
Locale locale = getLocale(context);
ClassLoader loader = getClassLoader();
String summary = getString(appBundle, bundleName, resourceId,
locale, loader, params);
if (summary == null) summary = "???" + resourceId + "???";
String detail = getString(appBundle, bundleName, resourceId + "_detail",
locale, loader, params);
return new FacesMessage(summary, detail);
}
public static String getString(String bundle, String resourceId,
Object[] params) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
String appBundle = app.getMessageBundle();
Locale locale = getLocale(context);
ClassLoader loader = getClassLoader();
return getString(appBundle, bundle, resourceId, locale, loader, params);
}
public static String getString(String bundle1, String bundle2,
String resourceId, Locale locale, ClassLoader loader,
Object[] params) {
String resource = null;
ResourceBundle bundle;
if (bundle1 != null) {
bundle = ResourceBundle.getBundle(bundle1, locale, loader);
if (bundle != null)
try {
resource = bundle.getString(resourceId);
} catch (MissingResourceException ex) {
}
}
if (resource == null) {
bundle = ResourceBundle.getBundle(bundle2, locale, loader);
if (bundle != null)
try {
resource = bundle.getString(resourceId);
} catch (MissingResourceException ex) {
}
}
if (resource == null) return null; // no match
if (params == null) return resource;
MessageFormat formatter = new MessageFormat(resource, locale);
return formatter.format(params);
}
public static Locale getLocale(FacesContext context) {
Locale locale = null;
UIViewRoot viewRoot = context.getViewRoot();
if (viewRoot != null) locale = viewRoot.getLocale();
if (locale == null) locale = Locale.getDefault();
return locale;
}
public static ClassLoader getClassLoader() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) loader = ClassLoader.getSystemClassLoader();
return loader;
}
public static String getString(FacesContext context, String resourceId) {
Application app = context.getApplication();
String appBundle = app.getMessageBundle();
Locale locale = getLocale(context);
ClassLoader loader = getClassLoader();
return getString(appBundle, locale, loader, resourceId);
}
public static String getString(String appBundle, Locale locale, ClassLoader loader, String resourceId) {
String resource = null;
ResourceBundle bundle;
if (appBundle != null) {
bundle = ResourceBundle.getBundle(appBundle, locale, loader);
if (bundle != null)
try {
resource = bundle.getString(resourceId);
} catch (MissingResourceException ex) {
}
}
if (resource == null) return null; // no match
else return resource;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -