📄 utils.java
字号:
/*
* 作者: 胡李青
* qq: 31703299
* Copyright (c) 2007 huliqing
* 主页 http://www.tbuy.biz/
* 你可以免费使用该软件,未经许可请勿作用于任何商业目的,如有技术问题请与本人联系!
*/
package biz.tbuy.common;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
import java.text.MessageFormat;
import javax.faces.context.FacesContext;
import javax.faces.application.FacesMessage;
/**
* @author huliqing
* <p><b>qq:</b>31703299
* <p><b>E-mail:</b><a href="mailto:huliqing.cn@gmail.com">huliqing.cn@gmail.com</a>
* <p><b>Homepage:</b><a href="http://www.tbuy.biz/">http://www.tbuy.biz/</a>
*/
public class Utils {
/** Creates a new instance of Utils */
public Utils() {
}
//*************************************************************************/
protected static ClassLoader getCurrentClassLoader(Object defaultObject) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = defaultObject.getClass().getClassLoader();
}
return loader;
}
/**
* 获取locale信息
* @return locale
*/
public static Locale getLocale() {
return FacesContext.getCurrentInstance().getViewRoot().getLocale();
}
/**
* 从资源束中获取指定的带有格式化的字符串
* @param bundleName
* @param id
* @param params
* @return text
*/
public static String getDisplayString (String bundleName,
String id,
Object[] params) {
String text = null;
ResourceBundle bundle;
try {
bundle = ResourceBundle.getBundle(bundleName, getLocale(),
getCurrentClassLoader(params));
} catch (Exception e) {
return id;
}
try {
text = bundle.getString(id);
} catch (MissingResourceException e) {
text = "[" + id + "]";
}
if (params != null) {
MessageFormat mf = new MessageFormat(text, getLocale());
text = mf.format(params, new StringBuffer(), null).toString();
}
return text;
}
/*************************************************************************/
public static void addMessage(String type, String bundleName, String id, Object[] params) {
FacesContext facesContext = FacesContext.getCurrentInstance();
String summary = getDisplayString(Constants.BUNDLE, type, null);
String detail = getDisplayString(bundleName, id, params);
FacesMessage message = null;
if (type.equals("Info")) {
message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
} else if(type.equals("Warn")) {
message = new FacesMessage(FacesMessage.SEVERITY_WARN, summary, detail);
} else if (type.equals("Error")) {
message = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail);
} else if (type.equals("Fatal")) {
message = new FacesMessage(FacesMessage.SEVERITY_FATAL, summary, detail);
}
facesContext.addMessage(null, message);
}
/**
* @param bundleName
* @param id
* @param params
*/
public static void addInfoMessage(String bundleName, String id, Object[] params) {
addMessage("Info", bundleName, id, params);
}
public static void addWarnMessage(String bundleName, String id, Object[] params) {
addMessage("Warn", bundleName, id, params);
}
public static void addErrorMessage(String bundleName, String id, Object[] params) {
addMessage("Error", bundleName, id, params);
}
public static void addFatalMessage(String bundleName, String id, Object[] params) {
addMessage("Fatal", bundleName, id, params);
}
/**
* @param bundleName 资源束名称
* @param id 要从资源束中获取的字符串的id
*/
public static void addInfoMessage(String bundleName, String id) {
addMessage("Info", bundleName, id, null);
}
public static void addWarnMessage(String bundleName, String id) {
addMessage("Warn", bundleName, id, null);
}
public static void addErrorMessage(String bundleName, String id) {
addMessage("Error", bundleName, id, null);
}
public static void addFatalMessage(String bundleName, String id) {
addMessage("Fatal", bundleName, id, null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -