Data from @RequestScoped bean is shared in different browsers

前端 未结 1 1447
半阙折子戏
半阙折子戏 2020-12-19 18:12

I have a @RequestScoped bean with a List property.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
imp         


        
相关标签:
1条回答
  • 2020-12-19 18:47

    You shouldn't manage a single bean by multiple different bean management frameworks like JSF, CDI and Spring. Choose the one or the other. When managing the bean by for example Spring's @Controller, all bean management related annotations of other frameworks like JSF's @ManagedBean and CDI's @Named are ignored.

    I don't do Spring and I have no idea why you're using it instead of the standard Java EE 6 API, but the symptoms and documentation indicates that the scope of such a Spring bean indeed defaults to the application scope. You need to specify the bean scope by Spring @Scope annotation. You would also like to remove the JSF bean management annotations since they have no value anymore anyway and would only confuse the developer/maintainer.

    @Controller
    @Scope("request")
    public class MyBean implements Serializable {
        // ...
    }
    

    Alternatively, you can also get rid of Spring @Controller annotation and stick to JSF @ManagedBean. You can use @ManagedProperty instead of @Autowired to inject another @ManagedBean instance or even a Spring managed bean (if you have Spring Faces EL resolver configured), or the Java EE standard @EJB to inject an @Stateless or @Stateful instance.

    E.g.

    @ManagedBean
    @RequestScoped
    public class MyBean implements Serializable {
    
        @EJB
        private SomeService service;
    
        // ...
    }
    

    See also:

    • Spring JSF integration: how to inject a Spring component/service in JSF managed bean?
    0 讨论(0)
提交回复
热议问题