通过预先创建FacesContext,在Filter中访问请求属性(在重定向之前在托管bean中设置)(Access request Attributes (Set in managed bean before redirect) in Filter by precreating FacesContext)

我在托管bean中设置请求属性,然后通过faces-config重定向请求,如下所示:

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

在此之后,我试图通过预先创建FacesContext来访问我的过滤器中的此请求属性

FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("foo");

最后无法在过滤器本身中获取此属性,但我能够非常轻松地在第二个托管bean中再次获得相同的属性。 有没有办法在过滤器中获得它?

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.getCurrentInstance().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?

最满意答案

两种方式:

存储在会话中,并在必要时过滤将其从会话中删除。

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

顺便说一句,没有必要在Filter自己创建FacesContext 。 只需将ServletRequest为HttpServletRequest 。

HttpSession session = ((HttpServletRequest) request).getSession(); String foo = (String) session.getAttribute("foo"); session.removeAttribute("foo");

使用ExternalContext#redirect()将其添加为请求参数。

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

然后在Filter :

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

Two ways:

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");

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");

更多推荐