java – JSF 2.0. Failed to get post parameters and cookies in prerenderview event handler
I'm trying to develop a service
The key is my index XHTML should get parameters (post and get) and cookies from HTTP request
I try to use it in combination with < F: metadata > and < F: event type = "prerenderview" > like this:
<f:Metadata> <f:event type="preRenderView" listener="#{deConversation.start}"/> </f:Metadata>
deConversation. Code for start:
public void start(ComponentSystemEvent event) { System.out.println("checkLogin"); HttpServletRequest request = SsoHelper.getRequest(); String requestSessId = SsoHelper.getRequestSessionId(request); String requestRedirect = SsoHelper.getRequestRedirect(request); System.out.println("sessId " + requestSessId); if (requestRedirect == null || requestRedirect.isEmpty()) { requestRedirect = "self"; } if (requestSessId != null) { trySessId(requestSessId,requestRedirect); } externalResourcesHandler.setExternalRedirect(requestRedirect); tryToBeginConversation(); if (!isAuthorized()) { SsoHelper.performNavigation("auth"); } }
Ssohelper only provides such APIs:
public static String getRequestSessionId(HttpServletRequest request) { Map<String,Object> cookieMap = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap(); String requestDeSessionId = null; if (cookieMap.containsKey("de_session_id")) { requestDeSessionId = ((Cookie) cookieMap.get("de_session_id")).getValue(); } return requestDeSessionId; } public static String getRequestRedirect(HttpServletRequest request) { return getRequestParam(request,"redirect","self"); } public static String getRequestExternalCss(HttpServletRequest request) { return getRequestParam(request,"externalcss",null); } public static String getRequestParam(HttpServletRequest request,String name,String defaultValue) { String[] paramValues = HttpServletRequestHelper.getParamValues(request,name); String paramValue = null; if (paramValues != null && paramValues.length != 0) { paramValue = paramValues[0]; } if(paramValue == null){ paramValue = defaultValue; } return paramValue; } public static HttpServletRequest getRequest() { return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); } public static void performNavigation(String destination) { FacesContext context = FacesContext.getCurrentInstance(); ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) context.getApplication().getNavigationHandler(); handler.performNavigation(destination); }
The point is that I can't get any post parameters or cookies in the method start() I can only get the get parameter
Use < F: event type = "prerenderview" >? Is it possible to read cookies and post parameters?
Solution
I think you should use @postconstruct to invoke the init method so that you can call init () before the page is rendered & use global variables to assign Session or Cookies values in variables so that you can fulfill your requirements.
Example:
@postconstruct public void init(){ // Your Response Map request = (Map)FacesContext.getCurrentInstance().getExternalContext().getResponse(); request.get("Your Key"); // Cookies Map cookie = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap(); cookie.get("Your Key"); Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap(); session.get("Your Key"); }
Try this. I think it will help you