Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
964 views
in Technique[技术] by (71.8m points)

jsf - Add items to List in Request Scoped Bean

I have a backing bean as following:

@Named
@RequestScoped
public class ClientNewBackingBean {

    @Inject
    private ClientFacade facade;
    private Client 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().

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);
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

    // ...

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";
    }

}

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;

    // ...

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;

    // ...

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...