让我们假设一个简单的Jsf示例,其中包含xhtml页面,ManagedBean,服务和JPA entityClass。我有很多具有以下结构的用例:
一些简单的例子,所以每个人都会理解
实体:
public class Entity {
private long id;
private boolean value;
...
// Getter and Setter
}
道:
public class EntityService {
// Entity Manger em and other stuff
public void enableEntity(long id) {
Entity e = em.find(id);
e.value = true;
em.persist(e);
}
}
Managed Bean:
@ManagedBean
@RequestScoped/ViewScoped
public class EntityBean() {
@EJB
private EntityService entityService;
private Entity entity;
@PostConstruct
public void init() {
// here i fetch the data, to provide it for the getters and setters
entity = entityService.fetchEntity();
}
public void enableEntity() {
entityService.enableEntity(entity.getId);
}
// Getter and Setter
}
最后是一个简单的xhtml:
<html>
// bla bla bla
<h:panelGroup id="parent">
<h:panelGroup id="disabled" rendered="#{not EntityBean.entity.value}>
<p:commandButton value="action" action="#{EntityBean.enableEntity}" update="parent" />
</h:panelGroup>
<h:panelGroup id="enabled" rendered="#{EntityBean.entity.value}>
// other stuff that should become visible
</h:panelGroup>
</h:panelGroup>
</html>
我想要实现的目标:
我已尝试过的内容
我的问题:
这对我来说似乎是一个根本问题,因为获取最新数据对我的应用程序至关重要(数据经常更改)。
使用Primefaces 6.1和Wildfly 10.x
答案 0 :(得分:1)
您如何看待这个? 将为更新创建的请求范围bean,并且每个请求只执行一次fetchEntity()。
<f:metadata>
<f:viewAction action="#{entityBean.load()}" onPostback="true"/>
</f:metadata>
@ManagedBean
@RequestScoped
public class EntityBean() {
@EJB
private EntityService entityService;
private Entity entity = null;
public void load() {}
public Entity getEntity() {
if(entity == null) {
entity = entityService.fetchEntity();
}
return entity;
}
public void enableEntity() {
entityService.enableEntity(getEntity().getId);
}
// Getter and Setter
}