JSF 2.0. Unable to get POST settings and cookies in preRenderView event handler

I am trying to create a service.

The fact is that my index.xhtml should receive parameters (POST and GET) and cookies from an HTTP request.

I tried a combination with <f:metadata> and <f:event type="preRenderView"> as follows:

 <f:metadata> <f:event type="preRenderView" listener="#{deConversation.start}"/> </f:metadata> 

Code for deConversation.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 simply provides the api as follows:

 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 fact is, I could not get any POST or cookie parameters in the start () method. I can only get GET parameters.

Is it possible to read cookies and POST parameters using <f:event type="preRenderView"> ?

+7
source share
2 answers

I think you should call the init method with @PostConstruct so that init () is called before the page is rendered and accepts a global variable to assign Session or Cookies in the variables (variables) so that you can achieve 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.

0
source

You can also use the f: viewParam tag to get the POST / GET parameter and bind it to the property of your backup bean.

More precisely, the tag calls the UIViewParameter binding as metadata for the current view. Assuming your POST parameter name is "mypostparam", you can do something like:

 <f:metadata> <f:viewParam id="mypostparam" name="mypostparam" value="#{deConversation.myPostParam}"/> <f:event type="preRenderView" listener="#{deConversation.start}"/> </f:metadata> 

In your bean support, you must declare the myPostParam property and standard setter / getter methods. As an advantage, you can install standard converters and validators so JSF takes care of this.

The only limit is that it does not work during the rendering response phase (you can get the POST parameter during navigation using the action method that returns String). He will work on a view that is the goal of sending a POST.

0
source

All Articles