简体   繁体   English

在JSF 2.0中刷新托管会话Bean

[英]Refresh managed session bean in JSF 2.0

After I commit some data into the database I want my session beans to automatically refresh themselves to reflect the recently committed data. 将一些数据提交到数据库后,我希望会话bean自动刷新自身以反映最近提交的数据。 How do I achieve this when using managed session beans in JSF 2.0? 在JSF 2.0中使用托管会话Bean时如何实现?

Currently I have to restart the web server in order for the sessions to clear and load anew again. 目前,我必须重新启动Web服务器,以便会话可以清除并重新加载。

2 ways: 2种方式:

  1. Put them in the view scope instead. 而是将它们放在视图范围内。 Storing view-specific data sessionwide is a waste. 在整个会话范围内存储特定于视图的数据是一种浪费。 If you have a performance concern, you should concentrate on implementing connection pooling, DB-level pagination and/or caching in the persistence layer (JPA2 for example supports second level caching). 如果您有性能方面的问题,则应集中精力在持久层中实现连接池,数据库级分页和/或缓存(例如,JPA2支持二级缓存)。

     @ManagedBean @ViewScoped public class FooBean { // ... } 

  2. Add a public load() method so that it can be invoked from the action method (if necessary, from another bean). 添加一个公共load()方法,以便可以从action方法(如有必要,从另一个bean)中调用它。

     @ManagedBean @SessionScoped public class FooBean { private List<Foo> foos; @EJB private FooService fooService; @PostConstruct public void load() { foos = fooService.list(); } // ... } 

    which can be invoked in action method inside the same bean (if you submit the form to the very same managed bean of course): 可以在同一bean内的action方法中调用它(如果您将表单提交到完全相同的托管bean中):

      public void submit() { fooService.save(foos); load(); } 

    or from an action method in another bean (for the case that your managed bean design is a bit off from usual): 或通过另一个bean中的操作方法(对于您的受管bean设计与平时有点不同的情况):

      @ManagedProperty("#{fooBean}") private FooBean fooBean; public void submit() { fooService.save(foos); fooBean.load(); } 

    This of course only affects the current session. 当然,这只会影响当前会话。 If you'd like to affect other sessions as well, you should really consider putting them in the view scope instead, as suggested in the 1st way. 如果您也想影响其他会话,则应该真正考虑将它们放在视图范围中,如第一种方法所建议。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM