Jsf 2 访问筛选器中的托管属性
在我试图找出此问题中所述问题的原因时,我想对我使用的用户会话筛选器计时。我已经创建了一个计时器,它是一个应用程序范围的beanJsf 2 访问筛选器中的托管属性,jsf-2,properties,servlet-filters,Jsf 2,Properties,Servlet Filters,在我试图找出此问题中所述问题的原因时,我想对我使用的用户会话筛选器计时。我已经创建了一个计时器,它是一个应用程序范围的bean 我的问题是,是否可以从过滤器中作为托管属性访问这个bean。在我的其他bean中,我可以这样做,尽管在过滤器中托管属性总是空的。更新的答案: 通过以下方式从筛选器访问应用程序范围内的bean ServletContext context = req.getServletContext(); MyAppBean myAppBean = (MyAppBean) contex
我的问题是,是否可以从过滤器中作为托管属性访问这个bean。在我的其他bean中,我可以这样做,尽管在过滤器中托管属性总是空的。更新的答案: 通过以下方式从筛选器访问应用程序范围内的bean
ServletContext context = req.getServletContext();
MyAppBean myAppBean = (MyAppBean) context.getAttribute("myAppBean");
通过以下方式从筛选器访问会话范围内的bean
HttpSession session = ((HttpServletRequest) req).getSession(false);
MyAppBean myAppBean = (MyAppBean ) session.getAttribute("myAppBean");
原始答案: 是的,您可以将其设置为
(eager=true)
,并添加所需的注释
@ManagedBean(eager = true)
@ApplicationScoped
public class MyAppBean { }
在你的sesion范围的bean中,像这样访问它
@ManagedProperty(value = "#{myAppBean }")
private MyAppBean myAppBean; //add getter and setter
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getApplicationMap().get("country");
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getSessionMap().get("country");
如果您的bean不是@ManagedBean
,您可以像这样从应用程序映射访问它
@ManagedProperty(value = "#{myAppBean }")
private MyAppBean myAppBean; //add getter and setter
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getApplicationMap().get("country");
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getSessionMap().get("country");
如果您想要访问的bean是一个@SessionScoped
,您可以像这样从SessionMap
中获取它
@ManagedProperty(value = "#{myAppBean }")
private MyAppBean myAppBean; //add getter and setter
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getApplicationMap().get("country");
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getSessionMap().get("country");
这里有一些关于如何从非托管bean访问bean的不错的教程
更新答案:
通过以下方式从筛选器访问应用程序范围内的bean
ServletContext context = req.getServletContext();
MyAppBean myAppBean = (MyAppBean) context.getAttribute("myAppBean");
通过以下方式从筛选器访问会话范围内的bean
HttpSession session = ((HttpServletRequest) req).getSession(false);
MyAppBean myAppBean = (MyAppBean ) session.getAttribute("myAppBean");
原始答案:
是的,您可以将其设置为(eager=true)
,并添加所需的注释
@ManagedBean(eager = true)
@ApplicationScoped
public class MyAppBean { }
在你的sesion范围的bean中,像这样访问它
@ManagedProperty(value = "#{myAppBean }")
private MyAppBean myAppBean; //add getter and setter
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getApplicationMap().get("country");
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getSessionMap().get("country");
如果您的bean不是@ManagedBean
,您可以像这样从应用程序映射访问它
@ManagedProperty(value = "#{myAppBean }")
private MyAppBean myAppBean; //add getter and setter
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getApplicationMap().get("country");
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getSessionMap().get("country");
如果您想要访问的bean是一个@SessionScoped
,您可以像这样从SessionMap
中获取它
@ManagedProperty(value = "#{myAppBean }")
private MyAppBean myAppBean; //add getter and setter
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getApplicationMap().get("country");
MyAppBean myAppBean = (MyAppBean ) FacesContext.getCurrentInstance().
getExternalContext().getSessionMap().get("country");
这里有一些关于如何从非托管bean访问bean的不错的教程
恐怕没什么区别。过滤器需要是一个bean才能工作吗?目前不是。它使用了您的上一个解决方案。但是,将筛选器注册为ManagedBean并没有成功。奇怪的谢谢你的帮助!恐怕没什么区别。过滤器需要是一个bean才能工作吗?目前不是。它使用了您的上一个解决方案。但是,将筛选器注册为ManagedBean并没有成功。奇怪的谢谢你的帮助!当你说“filter”时,你实际上是指javax.servlet.filter
?(如果是这样,您应该使用[servlet过滤器]
标记,而不是[filter]
!)在这种情况下,当前接受的答案没有意义。您是对的。我把标签换成了正确的。关于答案,最后一个建议奏效了。不过,我仍然不确定ManagedProperty机制在这种情况下是如何工作的。FacesContext
通常在servlet过滤器中根本不可用,因此我无法理解它为什么适用于您。托管属性仅在托管bean中工作,但是servlet筛选器不是托管bean。然后,您的问题基本上得到了与此问题相同的答案:感谢您启发我和链接中的示例。当您说“过滤器”时,您实际上是指javax.servlet.filter
?(如果是这样,您应该使用[servlet过滤器]
标记,而不是[filter]
!)在这种情况下,当前接受的答案没有意义。您是对的。我把标签换成了正确的。关于答案,最后一个建议奏效了。不过,我仍然不确定ManagedProperty机制在这种情况下是如何工作的。FacesContext
通常在servlet过滤器中根本不可用,因此我无法理解它为什么适用于您。托管属性仅在托管bean中工作,但是servlet筛选器不是托管bean。你的问题的答案基本上与这个问题的答案相同:谢谢你给我的启发和链接中的例子。