⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 facesutils.java

📁 库存管理系统:这是在Eclipse环境下开发的
💻 JAVA
字号:
package cn.hxex.library.view.util;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.FactoryFinder;
import javax.faces.application.Application;
import javax.faces.application.ApplicationFactory;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.webapp.UIComponentTag;
import javax.servlet.ServletContext;

public class FacesUtils
{
	/**
	 * Get servlet context.
	 * 
	 * @return the servlet context
	 */
	public static ServletContext getServletContext()
	{
		return (ServletContext) FacesContext.getCurrentInstance()
				.getExternalContext().getContext();
	}

	/**
	 * Get parameter value from request scope.
	 * 
	 * @param name
	 *            the name of the parameter
	 * @return the parameter value
	 */
	public static String getRequestParameter(String name)
	{
		return (String) FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get(name);
	}

	/**
	 * Evaluate the integer value of a JSF expression.
	 * 
	 * @param el
	 *            the JSF expression
	 * @return the integer value associated with the JSF expression
	 */
	public static Integer evalInt(String el)
	{
		if (el == null)
		{
			return null;
		}

		if (UIComponentTag.isValueReference(el))
		{
			Object value = getElValue(el);

			if (value == null)
			{
				return null;
			} else if (value instanceof Integer)
			{
				return (Integer) value;
			} else
			{
				return new Integer(value.toString());
			}
		} else
		{
			return new Integer(el);
		}
	}

	private static Application getApplication()
	{
		ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder
				.getFactory(FactoryFinder.APPLICATION_FACTORY);
		return appFactory.getApplication();
	}

	private static Object getElValue(String el)
	{
		return getValueBinding(el).getValue(FacesContext.getCurrentInstance());
	}

	private static ValueBinding getValueBinding(String el)
	{
		return getApplication().createValueBinding(el);
	}

	/**
	 * Add error message.
	 * 
	 * @param msg
	 *            the error message
	 */
	public static void addErrorMessage(String msg)
	{
		addErrorMessage(null, msg);
	}

	/**
	 * Add error message to a sepcific client.
	 * 
	 * @param clientId
	 *            the client id
	 * @param msg
	 *            the error message
	 */
	public static void addErrorMessage(String clientId, String msg)
	{
		FacesContext.getCurrentInstance().addMessage(clientId,
				new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
	}

	public static void addErrorMessageById(String messageId)
	{
		addErrorMessageById(messageId, null);
	}

	public static void addErrorMessageById(String messageId, Object params[])
	{
		FacesContext.getCurrentInstance().addMessage(null,
				getMessage(messageId, params, FacesMessage.SEVERITY_ERROR));
	}

	public static void addInfoMessageById(String messageId)
	{
		addInfoMessageById(messageId, null);
	}

	public static void addInfoMessageById(String messageId, Object params[])
	{
		FacesContext.getCurrentInstance().addMessage(null,
				getMessage(messageId, params, FacesMessage.SEVERITY_INFO));
	}

	public static FacesMessage getMessage(String messageId, Object params[],
			FacesMessage.Severity severity)
	{
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String bundleName = facesContext.getApplication().getMessageBundle();

		if (bundleName != null)
		{
			String summary = null;
			String detail = null;
			Locale locale = facesContext.getViewRoot().getLocale();
			ResourceBundle bundle = ResourceBundle.getBundle(bundleName,
					locale, getCurrentClassLoader(params));

			try
			{
				summary = bundle.getString(messageId);
				detail = bundle.getString(messageId + "_detail");
			} catch (MissingResourceException ex)
			{
			}
			if (summary != null)
			{
				MessageFormat mf = null;
				if (params != null)
				{
					mf = new MessageFormat(summary, locale);
					summary = mf.format(params, new StringBuffer(), null)
							.toString();
				}
				if (detail != null && params != null)
				{
					mf.applyPattern(detail);
					detail = mf.format(params, new StringBuffer(), null)
							.toString();
				}
				return (new FacesMessage(severity, summary, detail));
			}
		}

		return new FacesMessage(severity, "<<< " + messageId + " >>>", null);
	}

	protected static ClassLoader getCurrentClassLoader(Object defaultObject)
	{
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		if (loader == null)
		{
			loader = defaultObject.getClass().getClassLoader();
		}
		return loader;
	}

	/**
	 * Add information message.
	 * 
	 * @param msg
	 *            the information message
	 */
	public static void addInfoMessage(String msg)
	{
		addInfoMessage(null, msg);
	}

	/**
	 * Add information message to a sepcific client.
	 * 
	 * @param clientId
	 *            the client id
	 * @param msg
	 *            the information message
	 */
	public static void addInfoMessage(String clientId, String msg)
	{
		FacesContext.getCurrentInstance().addMessage(clientId,
				new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
	}

	/**
	 * Remove the managed bean based on the bean name.
	 * 
	 * @param beanName
	 *            the bean name of the managed bean to be removed
	 */
	public static void resetManagedBean(String beanName)
	{
		getValueBinding(getJsfEl(beanName)).setValue(
				FacesContext.getCurrentInstance(), null);
	}

	/**
	 * Get managed bean based on the bean name.
	 * 
	 * @param beanName
	 *            the bean name
	 * @return the managed bean associated with the bean name
	 */
	public static Object getManagedBean(String beanName)
	{
		Object o = getValueBinding(getJsfEl(beanName)).getValue(
				FacesContext.getCurrentInstance());

		return o;
	}

	private static String getJsfEl(String value)
	{
		return "#{" + value + "}";
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -