开发者

Access request Attributes (Set in managed bean before redirect) in Filter by precreating FacesContext

I am setting the request attribute in managed bean before redirect the request through faces-config as follows:

FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("foo","bar");

return "redirect_success";

After this i m trying to access this request attribute in my filter through pre creating FacesContext

FacesContext.getCurre开发者_高级运维ntInstance().getExternalContext().getRequestMap().get("foo");

Finally not able to get this attribute in filter itself but i am able to get the same attribute again in second managed bean very easily. Is there any way to get it in filter itself?


Two ways:

  1. Store in session and let filter remove it from session if necessary.

    externalContext.getSessionMap().put("foo", "bar");
    

    There's by the way no need to create FacesContext yourself in a Filter. Just cast ServletRequest to HttpServletRequest.

    HttpSession session = ((HttpServletRequest) request).getSession();
    String foo = (String) session.getAttribute("foo");
    session.removeAttribute("foo");
    
  2. Use ExternalContext#redirect() to add it as request parameter.

    externalContext.redirect("other.jsf?foo=bar");
    

    And then in Filter:

    String foo = ((HttpServletRequest) request).getParameter("foo");
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜