JSF2 无法从 ViewScoped 作为 ManagedProperty 访问 SessionScoped bean

发布于 2024-12-16 19:51:34 字数 1170 浏览 2 评论 0原文

我有一个奇怪的问题。 Afaik 我可以将 SessionScoped bean 注入到 viewscoped 中,因为它比另一个更广泛。这是我的代码:

@ManagedBean
@ViewScoped
public class ProjectBean implements Serializable {

@ManagedProperty(value="#{projectCurrentBean}")
private ProjectCurrentBean currentBean;

public void setCurrentBean(ProjectCurrentBean currentBean) {
    this.currentBean = currentBean;
}     

@ManagedProperty(value="#{userCredentialsBean}")
private UserCredentialsBean activeUser;

public void setActiveUser(UserCredentialsBean activeUser) {
    this.activeUser = activeUser;
}

2 个托管 bean:

@ManagedBean
@SessionScoped
public class ProjectCurrentBean implements Serializable  {

@ManagedBean
@SessionScoped
public class UserCredentialsBean  implements Serializable {

与 UserCredentialsBean 一起工作正常,但是当我放置 ProjectCurrentBean 时它失败:

Unable to create managed bean projectBean. The following problems were found: - The scope of the object referenced by expression #{projectCurrentBean}, request, is shorter   than the referring managed beans (projectBean) scope of view

为什么? :)

I have a strange problem. Afaik I can inject a SessionScoped bean into a viewscoped, because its broader, than the other. Here is my code:

@ManagedBean
@ViewScoped
public class ProjectBean implements Serializable {

@ManagedProperty(value="#{projectCurrentBean}")
private ProjectCurrentBean currentBean;

public void setCurrentBean(ProjectCurrentBean currentBean) {
    this.currentBean = currentBean;
}     

@ManagedProperty(value="#{userCredentialsBean}")
private UserCredentialsBean activeUser;

public void setActiveUser(UserCredentialsBean activeUser) {
    this.activeUser = activeUser;
}

The 2 managed bean:

@ManagedBean
@SessionScoped
public class ProjectCurrentBean implements Serializable  {

and

@ManagedBean
@SessionScoped
public class UserCredentialsBean  implements Serializable {

It works fine with the UserCredentialsBean, but when I put the ProjectCurrentBean it fails:

Unable to create managed bean projectBean. The following problems were found: - The scope of the object referenced by expression #{projectCurrentBean}, request, is shorter   than the referring managed beans (projectBean) scope of view

why? :)

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

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

发布评论

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

评论(1

很酷又爱笑 2024-12-23 19:51:34

您尚未使用 javax.faces.bean,而是来自 javax.enterprise.context。这不能与 javax.faces.bean 包中的 @ManagedBean 结合使用。然后,该 bean 将默认为请求范围,其行为类似于 <代码>@RequestScoped

修复您的进口。

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

@ManagedBean
@SessionScoped
public class ProjectCurrentBean implements Serializable {

You've not declared the bean using @SessionScoped from javax.faces.bean package, but instead from javax.enterprise.context package. This don't work in combination with @ManagedBean from javax.faces.bean package. The bean will then default to the request scope and behave like @RequestScoped.

Fix your imports.

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

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