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 ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 import javax.servlet.http.HttpSession ; 14 15 21 public class FacesUtils { 22 27 public static ServletContext getServletContext() { 28 return (ServletContext ) FacesContext.getCurrentInstance() 29 .getExternalContext().getContext(); 30 } 31 32 38 public static Object getManagedBean(String beanName) { 39 Object o = 40 getValueBinding(getJsfEl(beanName)).getValue(FacesContext.getCurrentInstance()); 41 42 return o; 43 } 44 45 50 public static void resetManagedBean(String beanName) { 51 getValueBinding(getJsfEl(beanName)).setValue(FacesContext.getCurrentInstance(), 52 null); 53 } 54 55 61 public static void setManagedBeanInSession(String beanName, 62 Object managedBean) { 63 FacesContext.getCurrentInstance().getExternalContext().getSessionMap() 64 .put(beanName, managedBean); 65 } 66 67 73 public static String getRequestParameter(String name) { 74 return (String ) FacesContext.getCurrentInstance().getExternalContext() 75 .getRequestParameterMap().get(name); 76 } 77 78 83 public static void addInfoMessage(String msg) { 84 addInfoMessage(null, msg); 85 } 86 87 93 public static void addInfoMessage(String clientId, String msg) { 94 FacesContext.getCurrentInstance().addMessage(clientId, 95 new FacesMessage(FacesMessage.SEVERITY_INFO, 96 msg, msg)); 97 } 98 99 104 public static void addErrorMessage(String msg) { 105 addErrorMessage(null, msg); 106 } 107 108 114 public static void addErrorMessage(String clientId, String msg) { 115 FacesContext.getCurrentInstance().addMessage(clientId, 116 new FacesMessage(FacesMessage.SEVERITY_ERROR, 117 msg, msg)); 118 } 119 120 126 public static Integer evalInt(String el) { 127 if (el == null) { 128 return null; 129 } 130 131 if (UIComponentTag.isValueReference(el)) { 132 Object value = getElValue(el); 133 134 if (value == null) { 135 return null; 136 } else if (value instanceof Integer ) { 137 return (Integer ) value; 138 } else { 139 return new Integer (value.toString()); 140 } 141 } else { 142 return new Integer (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 el) { 154 return getApplication().createValueBinding(el); 155 } 156 157 public static HttpServletRequest getRequest() { 158 return (HttpServletRequest ) FacesContext.getCurrentInstance() 159 .getExternalContext() 160 .getRequest(); 161 } 162 163 public static HttpServletResponse getResponse() { 164 return (HttpServletResponse ) FacesContext.getCurrentInstance() 165 .getExternalContext() 166 .getResponse(); 167 } 168 169 public static HttpSession getSession() { 170 return (HttpSession ) FacesContext.getCurrentInstance() 171 .getExternalContext() 172 .getSession(false); 173 } 174 175 private static Object getElValue(String el) { 176 return getValueBinding(el).getValue(FacesContext.getCurrentInstance()); 177 } 178 179 private static String getJsfEl(String value) { 180 return "#{" + value + "}"; 181 } 182 } 183 | Popular Tags |