将项目添加到请求范围Bean中的列表 [英] Add items to List in Request Scoped Bean

查看:55
本文介绍了将项目添加到请求范围Bean中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个支持bean,如下所示:

I have a backing bean as following:

@Named
@RequestScoped
public class ClientNewBackingBean {

    @Inject
    private ClientFacade facade;
    private Client client;

Client类除其他外具有List<Child> childrenList属性.将childrenList设置为new ArrayList()时,我可以创建一个新的Client.

The Client class has a List<Child> childrenList attribute, among others. I'm able to create a new Client when setting the childrenList with new ArrayList().

在视图中,我有一个输入文本字段和一个Add Child按钮.该按钮的属性actionListener=#{clientNewBackingBean.addChild()}实现为:

In the view, I have a input text field and an Add Child button. The button has the attribute actionListener=#{clientNewBackingBean.addChild()} implemented as:

public void addChild() {

    if(client.getChildrenList() == null) {
        client.getChildrenList(new ArrayList());
    }

    Child c = new Child("John Doe");

    client.getChildrenList().add(c);
}

每次单击Add Child按钮时,都会重新创建Bean,并且该视图仅显示一个John Doe的孩子(我认为,由于该对象属于Request范围).除了将bean作用域更改为Session之外,还有另一种方法可以解决此问题?

Everytime the Add Child button is clicked, the bean is recreated and the view only shows one John Doe child (due to it being Request scoped, I believe). Is there another way to solve this besides changing the bean scope to Session?

推荐答案

如果使用标准的JSF bean管理注释@ManagedBean,则可以通过

If you were using standard JSF bean management annotation @ManagedBean, you could have solved it by just placing the bean in the view scope by @ViewScoped.

@ManagedBean
@ViewScoped
public class ClientNewBackingBean implements Serializable {

    @EJB
    private ClientFacade facade;

    // ...

但是在CDI中,@ViewScoped不存在,最接近的替代方法是

In CDI, the @ViewScoped however doesn't exist, the closest alternative is @ConversationScoped. You only have to start and stop it yourself.

@Named
@ConversationScoped
public class ClientNewBackingBean implements Serializable {

    @Inject
    private Conversation conversation;

    // ...

    @PostConstruct
    public void init() {
        conversation.begin();
    }

    public String submitAndNavigate() {
        // ...

        conversation.end();
        return "someOtherPage?faces-redirect=true";
    }

}

您还可以使用CDI扩展 MyFaces CODI ,它将透明地桥接JSF @ViewScoped注释可以与@Named一起正常使用:

You can also use the CDI extension MyFaces CODI which will transparently bridge the JSF @ViewScoped annotation to work properly together with @Named:

@Named
@ViewScoped
public class ClientNewBackingBean implements Serializable {

    @Inject
    private ClientFacade facade;

    // ...

一种CODI替代方法是使用@ViewAccessScoped,只要后续请求引用的是相同的托管bean(无论所用的物理视图文件如何),它就一直存在.

A CODI alternative is to use @ViewAccessScoped which lives as long as the subsequent requests reference the very same managed bean, irrespective of the physical view file used.

@Named
@ViewAccessScoped
public class ClientNewBackingBean implements Serializable {

    @Inject
    private ClientFacade facade;

    // ...

另请参见:

  • 如何选择正确的bean作用域?
  • 推荐的JSF 2.0 CRUD框架
  • See also:

    • How to choose the right bean scope?
    • Recommended JSF 2.0 CRUD frameworks
    • 这篇关于将项目添加到请求范围Bean中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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