KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > util > FacesUtils


1 package org.appfuse.webapp.util;
2
3 import javax.faces.FactoryFinder;
4 import javax.faces.application.Application;
5 import javax.faces.application.ApplicationFactory;
6 import javax.faces.application.FacesMessage;
7 import javax.faces.context.FacesContext;
8 import javax.faces.el.ValueBinding;
9 import javax.faces.webapp.UIComponentTag;
10 import javax.servlet.ServletContext JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13 import javax.servlet.http.HttpSession JavaDoc;
14
15 /**
16  * Utility class for JavaServer Faces. Found in JavaWorld article:
17  * http://www.javaworld.com/javaworld/jw-07-2004/jw-0719-jsf.html
18  *
19  * @author <a HREF="mailto:derek_shen@hotmail.com">Derek Y. Shen</a>
20  */

21 public class FacesUtils {
22     /**
23      * Get servlet context.
24      *
25      * @return the servlet context
26      */

27     public static ServletContext JavaDoc getServletContext() {
28         return (ServletContext JavaDoc) FacesContext.getCurrentInstance()
29                                             .getExternalContext().getContext();
30     }
31
32     /**
33      * Get managed bean based on the bean name.
34      *
35      * @param beanName the bean name
36      * @return the managed bean associated with the bean name
37      */

38     public static Object JavaDoc getManagedBean(String JavaDoc beanName) {
39         Object JavaDoc o =
40             getValueBinding(getJsfEl(beanName)).getValue(FacesContext.getCurrentInstance());
41
42         return o;
43     }
44
45     /**
46      * Remove the managed bean based on the bean name.
47      *
48      * @param beanName the bean name of the managed bean to be removed
49      */

50     public static void resetManagedBean(String JavaDoc beanName) {
51         getValueBinding(getJsfEl(beanName)).setValue(FacesContext.getCurrentInstance(),
52                                                      null);
53     }
54
55     /**
56      * Store the managed bean inside the session scope.
57      *
58      * @param beanName the name of the managed bean to be stored
59      * @param managedBean the managed bean to be stored
60      */

61     public static void setManagedBeanInSession(String JavaDoc beanName,
62                                                Object JavaDoc managedBean) {
63         FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
64                     .put(beanName, managedBean);
65     }
66
67     /**
68      * Get parameter value from request scope.
69      *
70      * @param name the name of the parameter
71      * @return the parameter value
72      */

73     public static String JavaDoc getRequestParameter(String JavaDoc name) {
74         return (String JavaDoc) FacesContext.getCurrentInstance().getExternalContext()
75                                     .getRequestParameterMap().get(name);
76     }
77
78     /**
79      * Add information message.
80      *
81      * @param msg the information message
82      */

83     public static void addInfoMessage(String JavaDoc msg) {
84         addInfoMessage(null, msg);
85     }
86
87     /**
88      * Add information message to a sepcific client.
89      *
90      * @param clientId the client id
91      * @param msg the information message
92      */

93     public static void addInfoMessage(String JavaDoc clientId, String JavaDoc msg) {
94         FacesContext.getCurrentInstance().addMessage(clientId,
95                                                      new FacesMessage(FacesMessage.SEVERITY_INFO,
96                                                                       msg, msg));
97     }
98
99     /**
100      * Add error message.
101      *
102      * @param msg the error message
103      */

104     public static void addErrorMessage(String JavaDoc msg) {
105         addErrorMessage(null, msg);
106     }
107
108     /**
109      * Add error message to a sepcific client.
110      *
111      * @param clientId the client id
112      * @param msg the error message
113      */

114     public static void addErrorMessage(String JavaDoc clientId, String JavaDoc msg) {
115         FacesContext.getCurrentInstance().addMessage(clientId,
116                                                      new FacesMessage(FacesMessage.SEVERITY_ERROR,
117                                                                       msg, msg));
118     }
119
120     /**
121      * Evaluate the integer value of a JSF expression.
122      *
123      * @param el the JSF expression
124      * @return the integer value associated with the JSF expression
125      */

126     public static Integer JavaDoc evalInt(String JavaDoc el) {
127         if (el == null) {
128             return null;
129         }
130
131         if (UIComponentTag.isValueReference(el)) {
132             Object JavaDoc value = getElValue(el);
133
134             if (value == null) {
135                 return null;
136             } else if (value instanceof Integer JavaDoc) {
137                 return (Integer JavaDoc) value;
138             } else {
139                 return new Integer JavaDoc(value.toString());
140             }
141         } else {
142             return new Integer JavaDoc(el);
143         }
144     }
145
146     private static Application getApplication() {
147         ApplicationFactory appFactory =
148             (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
149
150         return appFactory.getApplication();
151     }
152
153     private static ValueBinding getValueBinding(String JavaDoc el) {
154         return getApplication().createValueBinding(el);
155     }
156
157     public static HttpServletRequest JavaDoc getRequest() {
158         return (HttpServletRequest JavaDoc) FacesContext.getCurrentInstance()
159                                                 .getExternalContext()
160                                                 .getRequest();
161     }
162     
163     public static HttpServletResponse JavaDoc getResponse() {
164         return (HttpServletResponse JavaDoc) FacesContext.getCurrentInstance()
165                                                 .getExternalContext()
166                                                 .getResponse();
167     }
168     
169     public static HttpSession JavaDoc getSession() {
170         return (HttpSession JavaDoc) FacesContext.getCurrentInstance()
171                                                 .getExternalContext()
172                                                 .getSession(false);
173     }
174
175     private static Object JavaDoc getElValue(String JavaDoc el) {
176         return getValueBinding(el).getValue(FacesContext.getCurrentInstance());
177     }
178
179     private static String JavaDoc getJsfEl(String JavaDoc value) {
180         return "#{" + value + "}";
181     }
182 }
183
Popular Tags