多次重构 JSF View Scoped Bean [英] JSF View Scoped Bean Reconstructed Multiple Times

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

问题描述

我认为 @ViewScoped 应该防止 bean 在用户在同一页面上时被重建......那么为什么我的 @ViewScoped JSf 控制器 bean甚至在操作处理程序导致浏览器导航离开该视图之前多次创建?

这里有人能指出我正确的方向吗?

这是我的代码:

视图(域/edit.xhtml)

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

ViewScoped 控制器 (DomainEdit.java)

@Named("domainEdit")@ViewScoped公共类 DomainEdit 实现了 Serializable {private static final long serialVersionUID = 1L;受保护的域编辑(){}@PostConstruct受保护的无效初始化(){System.out.println("post 构造被调用.");}@PreDestroy公共无效销毁(){System.out.println("预销毁调用.");}public DomainEntity getDomain() {System.out.println("显示域...");//一些返回域的代码返回域;}公共字符串保存(){System.out.println("保存...");//一些保存代码返回视图";}}

输出

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

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

    <块引用>

     后构造调用.显示域...预销毁调用.

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

    没有记录

  3. 点击保存"

<块引用>

 后构造调用.显示域...预销毁调用.后构造调用.显示域...预销毁调用.后构造调用.显示域...预销毁调用.后构造调用.显示域...预销毁调用.后构造调用.显示域...保存域...预销毁调用.

解决方案

除非您使用的是 JSF 2.2(目前尚未发布)或 MyFaces CODI(我原以为您会明确提及)那),@ViewScoped 在 CDI 中不起作用.这也非常符合您的问题症状.

通过 JSF 而不是 CDI 管理 bean.将 @Named("domainEdit") 替换为 javax.faces.bean 包中的 @ManagedBean.或者,安装 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 View Scoped Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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