Add items to List in Request Scoped Bean

前端 未结 2 1649
耶瑟儿~
耶瑟儿~ 2020-12-12 02:47

I have a backing bean as following:

@Named
@RequestScoped
public class ClientNewBackingBean {

    @Inject
    private ClientFacade facade;
    private Clien         


        
相关标签:
2条回答
  • 2020-12-12 03:24

    If you are using JSF 2, you should use ViewScoped bean.

    0 讨论(0)
  • 2020-12-12 03:48

    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:

    • How to choose the right bean scope?
    • Recommended JSF 2.0 CRUD frameworks
    0 讨论(0)
提交回复
热议问题