package com.nanometer.smartlab.util;
|
|
import org.primefaces.context.RequestContext;
|
import org.primefaces.expression.SearchExpressionFacade;
|
|
import javax.faces.application.FacesMessage;
|
import javax.faces.component.UIComponent;
|
import javax.faces.component.UIViewRoot;
|
import javax.faces.context.FacesContext;
|
import javax.servlet.http.Cookie;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
|
/**
|
* JSF的一些方法封装
|
*/
|
public class FacesUtils {
|
|
|
public static void info(String message) {
|
info("", message);
|
}
|
|
public static void warn(String message) {
|
warn("", message);
|
}
|
|
public static void error(String message) {
|
error("", message);
|
}
|
|
public static void fatal(String message) {
|
fatal("", message);
|
}
|
|
public static void info(String title, String message) {
|
addMessage(FacesMessage.SEVERITY_INFO, title, message);
|
}
|
|
public static void warn(String title, String message) {
|
addMessage(FacesMessage.SEVERITY_WARN, title, message);
|
}
|
|
public static void error(String title, String message) {
|
addMessage(FacesMessage.SEVERITY_ERROR, title, message);
|
}
|
|
public static void fatal(String title, String message) {
|
addMessage(FacesMessage.SEVERITY_FATAL, title, message);
|
}
|
|
public static void addMessage(FacesMessage.Severity severity, String title, String message) {
|
FacesContext.getCurrentInstance().addMessage(null,
|
new FacesMessage(severity, title, message));
|
}
|
|
/**
|
* 页面转到别的URL
|
*
|
* @param url 新的URL
|
* @throws IOException
|
*/
|
public static void redirect(String url) throws IOException {
|
FacesContext.getCurrentInstance().getExternalContext().redirect(url);
|
}
|
|
|
public static <T> T getBean(String beanName) {
|
FacesContext context = FacesContext.getCurrentInstance();
|
return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class);
|
}
|
|
/**
|
* 运行js语句
|
*
|
* @param js js代码语句
|
* @throws Exception
|
*/
|
public static void js(String js) {
|
RequestContext context = RequestContext.getCurrentInstance();
|
context.execute(js);
|
}
|
|
|
public static HttpServletRequest getHttpServletRequest() {
|
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
|
return request;
|
}
|
|
public static String getHttpRequestParameter(String name) {
|
return getHttpServletRequest().getParameter(name);
|
}
|
|
|
public static void ok() {
|
info("操作成功");
|
}
|
|
/**
|
* set cookie value
|
*
|
* @param name key of cookie
|
* @param value value of cookie
|
* @param expiry an integer specifying the maximum age of the
|
* cookie in seconds; if negative, means
|
* the cookie is not stored; if zero, deletes
|
* the cookie
|
*/
|
public static void setCookie(String name, String value, int expiry) {
|
FacesContext facesContext = FacesContext.getCurrentInstance();
|
HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
|
Cookie cookie = null;
|
Cookie[] cookies = request.getCookies();
|
if (cookies != null && cookies.length > 0) {
|
for (int i = 0; i < cookies.length; i++) {
|
if (cookies[i].getName().equals(name)) {
|
cookie = cookies[i];
|
break;
|
}
|
}
|
}
|
if (cookie != null) {
|
cookie.setValue(value);
|
} else {
|
cookie = new Cookie(name, value);
|
cookie.setPath(request.getContextPath());
|
}
|
cookie.setMaxAge(expiry);
|
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
|
response.addCookie(cookie);
|
}
|
|
/**
|
* get cookie value
|
*
|
* @param name key of the cookie
|
* @return value of the cookie
|
*/
|
public static String getCookie(String name) {
|
FacesContext facesContext = FacesContext.getCurrentInstance();
|
HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
|
Cookie[] cookies = request.getCookies();
|
if (cookies != null && cookies.length > 0) {
|
for (int i = 0; i < cookies.length; i++) {
|
if (cookies[i].getName().equals(name)) {
|
return cookies[i].getValue();
|
}
|
}
|
}
|
return null;
|
}
|
|
/**
|
* This method returns any UI component whose id is passed.
|
*
|
* @param clientId the id of the component
|
* @return the UIComponent
|
*/
|
public static UIComponent getUIComponent(String clientId) {
|
FacesContext facesContext = FacesContext.getCurrentInstance();
|
if (facesContext != null) {
|
UIViewRoot viewRoot = facesContext.getViewRoot();
|
if (viewRoot != null) {
|
return viewRoot.findComponent(clientId);
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 根据某个基准组件和相对这个组件的表达式,得到对应的组件
|
*
|
* @param mySourceComponent 基准组件
|
* @param expression 表达式
|
* @return 对应的组件
|
*/
|
public static UIComponent getComponentFromRelative(UIComponent mySourceComponent, String expression) {
|
FacesContext context = FacesContext.getCurrentInstance();
|
return SearchExpressionFacade.resolveComponent(context, mySourceComponent, expression);
|
}
|
|
/**
|
* 根据某个基准组件和相对这个组件的表达式,得到对应的组件
|
*
|
* @param clientId 基准组件ClientID
|
* @param expression 表达式
|
* @return 对应的组件
|
*/
|
public static UIComponent getComponentFromRelative(String clientId, String expression) {
|
UIComponent mySourceComponent = getUIComponent(clientId);
|
if (mySourceComponent == null) return null;
|
else return getComponentFromRelative(mySourceComponent, expression);
|
}
|
|
|
/**
|
* 根据某个基准组件和相对这个组件的表达式,得到对应的组件绝对路径ID
|
*
|
* @param mySourceComponent 基准组件
|
* @param expression 表达式
|
* @return 对应的组件ID
|
*/
|
public static String getComponentIDFromRelative(UIComponent mySourceComponent, String expression) {
|
UIComponent componentToUpdate = getComponentFromRelative(mySourceComponent, expression);
|
if (componentToUpdate == null) return null;
|
else return componentToUpdate.getClientId();
|
}
|
|
|
/**
|
* 根据某个基准组件和相对这个组件的表达式,得到对应的组件绝对路径ID
|
*
|
* @param mySourceComponentID 基准组件ClientID
|
* @param expression 表达式
|
* @return 对应的组件ID
|
*/
|
public static String getComponentIDFromRelative(String mySourceComponentID, String expression) {
|
UIComponent componentToUpdate = getComponentFromRelative(mySourceComponentID, expression);
|
if (componentToUpdate == null) return null;
|
else return componentToUpdate.getClientId();
|
}
|
|
}
|