Get Request and Session Parameters and Attributes from JSF pages

前端 未结 8 1766
轮回少年
轮回少年 2020-11-29 19:23

I\'m using JSF with facelets and I need to get the request and session parameters inside the JSF page. In JSP pages I got this parameter like that: \"${requestScope.pa

8条回答
  •  借酒劲吻你
    2020-11-29 19:46

    You can also use a bean (request scoped is suggested) and directly access the context by way of the FacesContext.

    You can get the HttpServletRequest and HttpServletResposne objects by using the following code:

    HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
    

    After this, you can access individual parameters via getParameter(paramName) or access the full map via getParameterMap() req object

    The reason I suggest a request scoped bean is that you can use these during initialization (worst case scenario being the constructor. Most frameworks give you some place to do code at bean initialization time) and they will be done as your request comes in.

    It is, however, a bit of a hack. ;) You may want to look into seeing if there is a JSF Acegi module that will allow you to get access to the variables you need.

提交回复
热议问题