JSF 会话 ManagedBean 在每个请求时实例化

发布于 2024-12-14 11:13:57 字数 317 浏览 4 评论 0原文

我的应用程序中有一个 @ManagedBean,带有 @sessionScoped 注释。

问题是,每个请求都会调用公共无参数构造函数,因此我的字段被重置,并且我的模型逻辑进入空间。

我尝试构建和清理,重置 glassFish,但仍然在每个请求时调用 bean 构造函数。 我还有一个 faces-config.xml 来控制页面导航。

我可以通过在 faces-config 中重新声明 bean 来解决这个问题,现在它可以工作了......

你知道为什么会发生这种情况吗?如果我删除 faces-config,问题仍然存在。

谢谢大家!

I have a @ManagedBean in my app, with the @sessionScoped anotation.

The problem is that the public no arg constructor is being called for each request, so my fields are being reseted and my model logic goes to space.

I tried build and clean, reset glassFish, but still the bean constructor get's called at each request.
I also have a faces-config.xml to control the page navigation.

I could solve this by redeclaring the bean inside the faces-config, now it work's...

Any ideas of why that's happening? If i delete the faces-config, the problem persists.

Thanks everybody!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人如玉 2024-12-21 11:13:57

如果 @SessionScoped 不属于 javax.faces.bean 包,但不是完全不同的 API,例如 javax.enterprise.context 包。仔细检查您的导入并小心 IDE 自动完成。 IDE 倾向于按包名称对自动建议进行排序,因此 javax.enterprise.context 将显示为第一个选项。

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

@ManagedBean
@SessionScoped
public class Bean {
    // ...
}

如果您没有在 @ManagedBean 上使用任何 javax.faces.bean 范围注释,则该 bean 将默认为 @NoneScoped,这意味着它是在每个单独的#{bean} EL 评估。

另请参阅:

That can happen if @SessionScoped is not of the javax.faces.bean package, but instead of a completely different API, for example the javax.enterprise.context package. Doublecheck your imports and be careful with IDE autocompletion. IDEs tend to sort autosuggestions by package name and thus the javax.enterprise.context would appear as 1st option.

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

@ManagedBean
@SessionScoped
public class Bean {
    // ...
}

If you aren't using any javax.faces.bean scope annotation on a @ManagedBean, then the bean will default to @NoneScoped, which means that it's constructed on every single #{bean} EL evaluation.

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文