儘管它是「普遍灰心」(?由誰),我使用的東西在我的JSF-2.2的支持豆以下技術之一(這取決於我是否需要personId
其他東西):
@ViewScoped
public class BeanConverter {
@Inject @Param(name = "personId")
private ParamValue<Person> curPerson;
}
@ViewScoped
public class BeanConstruct {
@PersistenceContext
private EntityManager em;
@Inject @Param
private ParamValue<Long> personId;
private Person curPerson;
@PostConstruct
public void init() {
curPerson = em.find(Person.class, personId.getValue());
}
}
這使用了優秀的CDI支持Omnifaces。 然後我使用merge()
來更新實體,但在我的情況下,只有一個bean可以保存對實體的更改,所以YMMV。當豆類需要相互通信更新或實體的作品,我通常去javax.enterprise.Event
S其中,事件得到實體構造函數的參數:
public class BeanSending {
@Inject
private Event<PersonCreated> personCreatedEvent;
public void constructPerson() {
Person person = makePerson();
personCreatedEvent.fire(new PersonCreated(person));
}
}
public class BeanUpdater {
public void updatePerson(@Observes PersonCreated evt) {
doStuffWithPerson(evt.getPerson());
}
}
爲什麼不把實體對象保存在會話中並在所有的bean中使用它。 – Adarsh
我不想膨脹會話。我正在尋找更適合JSF框架的解決方案。在會話中存儲某些內容聽起來像是一種解決方法。 –
當然,你需要一個'@ SessionScoped' /'@ ApplicationScoped'管理bean。關於被通知的豆,你需要更具體。 –