Passing parameters between Request Scoped Managed Beans in JSF + EJB 3.1

依然范特西╮ 提交于 2019-12-12 03:48:29

问题


Our problem is a very basic, simple implementation of editing the database using JSF + EJB.

Keeping things short: two separate XHTML views, use two separate Managed Beans @RequestScope.

WebuserListBean and EditWebuserBean, and with @ManagedProperty we inject WebuserListBean, so we could obtain selected user data. So far no problems. Views are filled with data succesfully!

BUT! We want to be able to edit the user! And here (to my surprise) we cannot overcome the problem.

1st try: Because the request scoped bean is dead after filling the view, on Save() method the @PostConstruct tries to launch again, of course it can't. So we couldn't even obtain it from database or anything.

2nd try: Because the request scoped bean is dead after filling the view, if we do not set up user as field in @postconstruct, we lose our connection with user object which was linked on previous view (and injected, but now that view is dead too).

3rd try: Cannot inject RequestScope in ViewScoped

Ok and our restrictions, because we think it's wrong way:

  1. We dont want to create a SessionScoped Managed Bean for this
  2. We dont want to use any params etc. We want to use EJB
  3. We are not sure if we could store data in Stateful session bean which is our endpoint for the module? Is it proper approach?

Thanks for any advice, we could paste some code but i guess it is pointless! Cheers!


回答1:


There are many ways to do it, but I recommend using the flash if the pages involved in the navigation are in the same folder (I recently found out reading a BalusC answer that there is a known issue with the flash, in which it will not hold values when navigating between pages in different folders!).

The flash is a way to hold parameters for a little longer than the context of a single request (concept taken from Ruby if I'm not mistaken, someone correct me if I'm wrong), allowing for the sent parameters to be fetched in a subsequent view, for example. Those values are discarded in the second request issued after saving them, if I'm not mistaken. You can inject the flash in your managed beans like this:

@ManagedProperty("#{flash}")
private Flash flash;

public void setFlash(Flash newFlash) {
    flash = newFlash;
}

Then, you access it like a map with the put and get methods. If you use the put method in a bean, return a redirection rule and, on the second bean, use the get method your object should be there.

You can also find a highly comprehensible guide of communication in JSF (listing a really extensive list of options) here, in particular if you need to navigate between pages in different folders.



来源:https://stackoverflow.com/questions/11634141/passing-parameters-between-request-scoped-managed-beans-in-jsf-ejb-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!