FacesUtil

 

public class FacesUtil {
    private static Logger logger = Logger.getLogger(FacesUtil.class);

    public static Object getValueExpression(String expressionString, Class cla) {
        FacesContext fc = FacesContext.getCurrentInstance();
        ValueBinding vb = fc.getApplication().createValueBinding(expressionString);//"#{"beanName"}"
        Object o = null;
        if (vb != null) {
            o = vb.getValue(fc);
        }
        return o;
    }

    public static Object getBackBeanByName(String backBeanName) {
        FacesContext fc = FacesContext.getCurrentInstance();
        VariableResolver variableResolver;
        variableResolver = fc.getApplication().getVariableResolver();
        return variableResolver.resolveVariable(fc, backBeanName);       
    }
   
   
    public static Application getApplication() {
        FacesContext fc = FacesContext.getCurrentInstance();
        if (fc != null) {
            return fc.getApplication();
        } else {
            return null;
        }
    }

    /**
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        if (FacesContext.getCurrentInstance() != null) {
            return FacesContextUtils.getRequiredWebApplicationContext(FacesContext.getCurrentInstance());
        } else {

        }
        return FacesContextUtils.getRequiredWebApplicationContext(FacesContext.getCurrentInstance());
    }

    /**
     * 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);
    }

    public static String getContextPath() {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
    }

    public static String getAssetsPath() {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/assets/";
    }

    public static String getAssetsRootPath() {
        return "assets/";
    }

    /**
     * Get request
     *
     * @return the request object
     */
    public static HttpServletRequest getRequest() {
        return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    }

    /**
     * Get response
     *
     * @return the response object
     */
    public static HttpServletResponse getResponse() {
        return (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
    }

    /**
     * Get Sesstion
     *
     * @return the sesstion object
     */
    public static HttpSession getSession() {
        if (FacesContext.getCurrentInstance() == null) {
            return null;
        } else {
            return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        }

    }

    public static void setSessionAttribute(String name,Object obj) {
        HttpSession s = getSession();       
        if (s != null) {           
             s.setAttribute(name,obj);
        }
    }
   
    public static Object getSessionAttribute(String name) {
        HttpSession s = getSession();
        if (s == null) {
            return null;
        } else {
            return s.getAttribute(name);
        }

    }

    public static UserSecurityMetaVo getUserSecurityMeta() {
        return (UserSecurityMetaVo) FacesUtil.getSession().getAttribute(Constants.USER_SECURITY_META);
    }

    /**
     * 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 id
     * @param msg
     *            the information message
     */
    public static void addInfoMessage(String id, String msg) {
        FacesContext.getCurrentInstance().addMessage(id, new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
    }

    /**
     * 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 id
     * @param msg
     *            the error message
     */
    public static void addErrorMessage(String id, String msg) {
        FacesContext.getCurrentInstance().addMessage(id, new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
    }  
   
    public static String getConfigurationItem(String name) {
        ConfigurationService s=(ConfigurationService)Context.getBean("configurationService");
        return s.getConfigtationValue(name);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值