【发布时间】:2021-12-29 12:20:43
【问题描述】:
我正在(主要)使用@ViewScoped @Named Faces bean(javax.faces.view.ViewScoped 和 javax.inject.Named)并且想知道是否可以从 ServletRequest(或 HttpServletRequest)中区分同一个 bean 的两个不同实例在javax.servlet.Filter内
例如,用户在两个不同的选项卡中打开同一个页面,但通过调用httpRequest.getSession().getId(),这两个选项卡具有相同的 SessionID。目前这对我没有帮助。
或者,我可以访问线程 ID,但这会随着每次 ajax 调用而改变,并且我需要在所有调用中为支持 bean 的实例保留一些唯一的东西......直到 bean 被销毁。
所以,这样的事情(这不起作用,但希望能说明我的需要)
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
try
{
HttpServletRequest httpRequest = (HttpServletRequest) request;
// not real code, but this is what I'm looking for;
// get a unique identifier for just this instance of the bean that stays the same across all ajax requests
String beanId = request.getCurrentBackingBean.getInstanceId();
...
【问题讨论】:
-
您确定 servlet 过滤器首先是您心目中工作的正确工具吗? xyproblem.info
-
@BalusC 目前在过滤器中,我正在使用 ThreadContext 为 log4j2 添加键/值,例如
ThreadContext.put("sessionId", session.getId());并希望为支持 bean ID 提供一个简单的类似解决方案。我对其他方式持开放态度,但不确定如何处理。 -
我认为您可以使用相位侦听器,@BalusC 在其他答案stackoverflow.com/questions/8388854/… 中解释了如何实现它
-
啊。如果您使用服务器端状态保存,
javax.faces.ViewState请求参数可能就足够了。
标签: jsf servlets httprequest servlet-filters