JSF查看范围bean重建多次 [英] JSF View Scoped Bean Reconstructed Multiple Times

查看:104
本文介绍了JSF查看范围bean重建多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为 @ViewScoped 应该是防止在用户在同一个页面上重建bean ...那么为什么我的 @ViewScoped JSF控制器bean即使在动作处理程序导致浏览器远离该视图之前多次创建?



任何人都可以指出我这是正确的方向?



这是我的代码:



View(domain / edit.xhtml)



 < h:form prependId =false> 
< h:inputText id =descriptionFieldvalue =#{domainEdit.domain.description}/>
< h:commandButton id =saveButtonvalue =saveaction =#{domainEdit.save}/>
< / h:form>



ViewScoped控件(DomainEdit.java)



  @Named(domainEdit)
@ViewScoped
public class DomainEdit implements Serializable {

private static final long serialVersionUID = 1L;


protected DomainEdit(){
}

@PostConstruct
protected void init(){
System.out。 println(post construct called);
}

@PreDestroy
public void destroy(){
System.out.println(pre destroy called);
}

public DomainEntity getDomain(){
System.out.println(显示域...);

//返回域的一些代码
返回域;
}

public String save(){
System.out.println(saving ...);

//一些保存代码

returnview;
}
}



输出



当我部署它并执行以下操作时,我得到以下输出:


  1. 导航到编辑视图edit.xhtml)


      
    显示域...
    pre destroy调用。



  2. 更改domainDescriptionField输入文本的内容

    没有记录


  3. 点击保存





  
显示域...
pre destroy调用。

结构调用。
显示域...
pre destroy调用。

结构调用。
显示域...
pre destroy调用。

结构调用。
显示域...
pre destroy调用。

结构调用。
显示域...
保存域...
预毁灭调用。



解决方案

重新使用JSF 2.2(目前还没有出现在这个时刻)或MyFaces CODI(我预期你会明确提到), @ViewScoped 在CDI工作。这也很符合您的问题症状。



通过JSF而不是CDI来管理bean。将 @Named(domainEdit) @ManagedBean javax.faces.bean 包。或者,安装MyFaces CODI将JSF @ViewScoped 链接到CDI。


I thought @ViewScoped was supposed to prevent the bean from being reconstructed while the user is on the same page... So why is my @ViewScoped JSf controller bean being created multiple times even before the action handler causes the browser to navigate away from that view?

Can anyone point me in the right direction here?

Here is my code:

The View (domain/edit.xhtml)

<h:form prependId="false">
    <h:inputText id="descriptionField" value="#{domainEdit.domain.description}" />
    <h:commandButton id="saveButton" value="save" action="#{domainEdit.save}" />
</h:form>

The ViewScoped controller (DomainEdit.java)

@Named("domainEdit")
@ViewScoped
public class DomainEdit implements Serializable {

    private static final long serialVersionUID = 1L;


    protected DomainEdit() {
    }

    @PostConstruct
    protected void init() {
        System.out.println("post construct called.");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("pre destroy called.");
    }

    public DomainEntity getDomain() {
        System.out.println("displaying domain...");

        // some code to return the domain
        return domain;
    }

    public String save() {
        System.out.println("saving...");

        // some saving code

        return "view";
    }
}

Output

I get the following output when I deploy this and perform the following:

  1. Navigate to the edit view (edit.xhtml)

       post construct called.
       displaying domain...
       pre destroy called.
    

  2. Change the content of the domainDescriptionField input text

    nothing logged

  3. Click 'save'

  post construct called.
  displaying domain...
  pre destroy called.

  post construct called.
  displaying domain...
  pre destroy called.

  post construct called.
  displaying domain...
  pre destroy called.

  post construct called.
  displaying domain...
  pre destroy called.

  post construct called.
  displaying domain...
  saving domain...
  pre destroy called.

解决方案

Unless you're using JSF 2.2 (which is still not out yet at this moment) or MyFaces CODI (which I'd have expected that you would explicitly mention that), the @ViewScoped doesn't work in CDI. This also pretty much matches your problem symptoms.

Manage the bean by JSF instead of CDI. Replace @Named("domainEdit") by @ManagedBean from javax.faces.bean package. Or, install MyFaces CODI to bridge JSF @ViewScoped to CDI.

这篇关于JSF查看范围bean重建多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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