Coming Up for Air

FacesUtil: A missing, yet important piece

Monday, May 08, 2006 |

A reader brought to my attention that I have never posted the code to FacesUtil, a convenience class used, for example, in my JSF, PhaseListeners, and GET Requests article, so I’ll fix that oversight now. Before I get to the code, though, let me preface it by saying this: This code has grown as several developers have hacked on it, so it my not be consistent, and probably doesn’t embody any sort of best practices. It does, however, work well for us, and that’s our primary concern. :) Note also, that this code has not been updated to the 1.2 specification yet, so you’ll at least get warnings if not errors should you use this in a 1.2 environment. I am currently in the process of updating the class, but, for now, here it is in its current state.

FacesUtil.java

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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;
import javax.servlet.http.HttpServletRequest;
/**
 * Utility class for JavaServer Faces.
 *
 */
public class FacesUtil {
    /**
     * Get servlet context.
     *
     * @return the servlet context
     */
    public static ServletContext getServletContext() {
        return (ServletContext) FacesContext.getCurrentInstance()
                .getExternalContext().getContext();
    }
    /**
     * 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;
    }
    /**
     * 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);
    }
    /**
     * Store the managed bean inside the session scope.
     *
     * @param beanName
     *            the name of the managed bean to be stored
     * @param managedBean
     *            the managed bean to be stored
     */
    public static void setManagedBeanInSession(String beanName,
            Object managedBean) {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
                .put(beanName, managedBean);
    }
    /**
     * 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);
    }
    /**
     * 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));
    }
    /**
     * 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));
    }
    /**
     * Add warning message.
     *
     * @param msg
     *            the warning message
     */
    public static void addWarningMessage(String msg) {
        addWarningMessage(null, msg);
    }
    /**
     * Add warning message to a sepcific client.
     *
     * @param clientId
     *            the client id
     * @param msg
     *            the warning message
     */
    public static void addWarningMessage(String clientId, String msg) {
        FacesContext.getCurrentInstance().addMessage(clientId,
                new FacesMessage(FacesMessage.SEVERITY_WARN, msg, msg));
    }
    /**
     * 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());
            }
        }
        return new Integer(el);
    }
    private static Application getApplication() {
        ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder
                .getFactory(FactoryFinder.APPLICATION_FACTORY);
        return appFactory.getApplication();
    }
    private static ValueBinding getValueBinding(String el) {
        return getApplication().createValueBinding(el);
    }
    public static HttpServletRequest getServletRequest() {
        return (HttpServletRequest) FacesContext.getCurrentInstance()
                .getExternalContext().getRequest();
    }
    private static Object getElValue(String el) {
        return getValueBinding(el).getValue(FacesContext.getCurrentInstance());
    }
    private static String getJsfEl(String value) {
        return "#{" + value + "}";
    }
}

Search

    Quotes

    Sample quote

    Quote source

    About

    My name is Jason Lee. I am a software developer living in the middle of Oklahoma. I’ve been a professional developer since 1997, using a variety of languages, including Java, Javascript, PHP, Python, Delphi, and even a bit of C#. I currently work for Red Hat on the WildFly/EAP team, where, among other things, I maintain integrations for some MicroProfile specs, OpenTelemetry, Micrometer, Jakarta Faces, and Bean Validation. (Full resume here. LinkedIn profile)

    I am the president of the Oklahoma City JUG, and an occasional speaker at the JUG and a variety of technical conferences.

    On the personal side, I’m active in my church, and enjoy bass guitar, running, fishing, and a variety of martial arts. I’m also married to a beautiful woman, and have two boys, who, thankfully, look like their mother.

    My Links

    Publications