【问题标题】:@SessionScoped bean with @ViewScoped behaviour具有 @ViewScoped 行为的 @SessionScoped bean
【发布时间】:2012-11-02 04:05:36
【问题描述】:

我的会话范围 bean 出现了奇怪的行为。我使用以下导入和注释使其成为会话范围:

编辑:更多代码

    import javax.enterprise.context.SessionScoped;
    import javax.inject.Named;

    @Named
    @SessionScoped
    public class DetailsBean implements Serializable {

    private LinkedHashMap<String, String> folder;
    @Inject
    private ApplicationBean appBean;
    @Inject
    private UserBean userBean;

    @PostConstruct
    public void resolveID() {
    this.folder = new LinkedHashMap<String, String>();
    for (LinkedHashMap<String, String> tempfolder : appBean.getRepositoryContent()) {
        if (tempfolder.get("text:nodeid").equals(URLid)) {
            this.folder = tempfolder;
          }
        }
    }

JSF 页面代码片段:

    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:rich="http://richfaces.org/rich">
    <f:metadata>
      <f:viewParam name="id" value="#{detailsBean.URLid}" required="true" requiredMessage="You must provide an Object Id"/>
      <f:event type="preRenderView" listener="#{detailsBean.resolveID}" />
    </f:metadata>
    <h:head>
      <title>Dataset #{detailsBean.name}</title>
    </h:head>
    <h:body>
     <h:form>
        <h:panelGrid columns="2" columnClasses="fixed-column">
            Name <h:inputText value="#{detailsBean.name}"
                              id="name" required="true"
                              requiredMessage="name required"/>
            <rich:message for="name" ajaxRendered="true"/>
        </h:panelGrid>
    </h:body>
    </h:form>
    </html>

现在,当我单击 jsf 页面中的链接时,会实例化这样的 DetailsBean。当我单击具有不同内容的另一个链接时,将使用相同的 bean,因为我仍在同一个会话中。现在奇怪的是,即使我创建了 2 个不同的浏览器选项卡,即使在刷新页面后它们也会显示不同的内容。同一个 bean 实例如何显示不同的内容?我认为通常只有 @ViewScoped bean 可以实现这一点?不要误会我的意思,我确实希望它们显示不同的内容,所以在这里使用 @ViewScoped 是正确的决定,但我只是想知道这怎么可能......

EDIT2:当我使用 javax.faces.ViewScoped 时,上面的代码不再起作用(然后由于 LinkedHashMap,我得到 java.io.NotSerializableException)

【问题讨论】:

  • 这应该是不可能的,但是如果你的支持者中有一些你不知道的逻辑在发生,那么它是可能的。请发布更多代码。
  • 好的,我编辑了。 jsf页面上的内容使用文件夹映射,并从中获取Strings。
  • 你能贴出使用这个backing bean的视图代码的sn-p吗?
  • 我做了...但似乎只有观点不对。当我在详细信息网站上进行更改时,只会更新最后加载的网站

标签: jsf jakarta-ee jsf-2 javabeans scopes


【解决方案1】:

我相信您在导入行中有错误。 import javax.enterprise.context.SessionScoped;。您应该从javax.faces.bean.SessionScoped 导入注释。

【讨论】:

  • OP 通过 CDI @Named 管理 bean,而不是通过 JSF @ManagedBean。即使那样,如果导入确实是错误的,它会默认请求范围,因此绝对不会像视图范围那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-16
  • 1970-01-01
  • 2018-08-05
  • 1970-01-01
  • 2011-03-23
  • 2014-10-20
  • 2012-02-07
相关资源
最近更新 更多