JSF为每个请求创建一个新的SessionScoped Bean [英] JSF creates a new SessionScoped Bean for every request

查看:96
本文介绍了JSF为每个请求创建一个新的SessionScoped Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:在每个http请求中,都会创建一个新的会话作用域bean,但我不知道为什么.

My problem is that one : at every http request, a new session scoped bean is created and I don't know why.

这是我的jsf索引页面:

Here is my jsf index page :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">

<h:head>
    <link type="text/css" rel="stylesheet" href="css/default.css"/>
</h:head>
<h:body>

    <p:growl autoUpdate="true" showDetail="true" globalOnly="true"/>

    <h:form id="f_main">

        <ui:include src="#{pageBean.page}.xhtml"/>

    </h:form>

</h:body>
</html>

这是我的PageBean

Here is my PageBean

package web.bean.system;

import org.apache.log4j.Logger;
import web.bean.AbstractBean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class PageBean extends AbstractBean {
    private static final long serialVersionUID = -882977117976414497L;
    private static final Logger LOG = Logger.getLogger(PageBean.class);
    public static final String HOME = "home";
    private static int IT = 0;

    private String page;

    public PageBean() {
        LOG.debug(IT++);
        this.page = HOME;
    }

    public String getPage() {
        LOG.debug(page);
        return this.page;
    }

    public void setPage(String page) {
        LOG.debug(page);
        this.page = page;
    }
}

在这种情况下,主页为空.

In this case, the home page is empty.

但是当我在刷新很多时间后查看日志时,我可以看到为每个http请求都创建了一个新bean.

But when I take a look at the logs after refreshing a lot of time, I can see that a new bean is created for every http request.

我已经证实我确实使用javax.faces.bean而不是其他软件包,但是我不知道为什么它不起作用...

I have verified that I realy use javax.faces.bean and not an other package but I don't know why It doesn't work...

您对我有什么解决办法吗?

Have you any solution for me ?

推荐答案

如果在客户端和服务器之间未正确维护HTTP会话,则可能会发生这种情况.首先,请先仔细阅读此答案的"HttpSession"部分,以首先了解HTTP会话的工作方式:

That may happen if the HTTP session is not properly maintained between the client and the server. Before all, first learn how HTTP sessions work by carefully reading the "HttpSession" section of this answer: How do servlets work? Instantiation, sessions, shared variables and multithreading.

现在,您应该了解它们默认情况下由Cookie支持.您现在还应该了解,如果由于某种原因客户端未维护cookie或服务器立即破坏了cookie,则不会在请求之间维护会话.

Now, you should understand that they're by default backed by cookies. You should now also understand that if cookies are not maintained by the client or instantly destroyed by the server for some reason, then the session won't be maintained across requests.

如果您使用的是带有内置Web开发人员工具集的现代Web浏览器,请按F12键显示它,然后打开网络"/网络"选项卡.在响应标头中查找Set-Cookie,在后续请求标头中查找Cookie.如果请求标头中没有Cookie,因此服务器在响应上返回了新的Set-Cookie标头,则意味着客户端不支持cookie.或者,如果存在正确的Cookie标头,并且服务器仍在每个响应上返回新的Set-Cookie标头,则意味着服务器的代码中某行在每个请求上均调用HttpSession#invalidate()(也许是本地认证过滤器)由入门者编写).

If you're using a bit modern webbrowser with builtin web developer toolset, press F12 to show it up and open the "Net"/"Network" tab. Look in the response header for Set-Cookie and in subsequent request header for Cookie. If Cookie is absent in request header and thus the server returns a new Set-Cookie header on the response, then it means that the client does not support cookies. Or if the right Cookie header is present and the server still returns a new Set-Cookie header on every response, then it means that the server's code has somewhere a line which calls HttpSession#invalidate() on every request (perhaps a homegrown authentication filter which is written by a starter).

这篇关于JSF为每个请求创建一个新的SessionScoped Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆