1

I have view scope search and edit beans which are created by:

javax.faces.bean.ManagedBean

javax.faces.bean.ViewScoped

When the user makes search and view the search results, he select a result and open it in new window and after the saving changes I want to update the results in search bean, so when I try to get the search bean using any of the following methods:

public static <T> T getBean(String beanName) {
        FacesContext context = FacesContext.getCurrentInstance();
        return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class);
    }

public static <T> T getViewScopeBean(String beanName){
  Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
  return (T) viewMap.get(beanName);
}

it returns null and can't find the search bean for the second method, and for the first method it creates a new instance of the search bean (although the search page is still opened), when I printed all the values in the map it doesn't contain the search bean. How can I get the search bean?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
  • @Jaqen H'ghar yes, tried that and it doesn't create new instance but it all the variables in the bean returns default values. – Mahmoud Saleh Mar 30 '15 at 11:26
  • What version of JSF are you running? – kolossus Mar 30 '15 at 18:49
  • @Jaqen H'ghar, i open a new window with javascript – Mahmoud Saleh Apr 01 '15 at 13:04
  • @kolossus version 2.2.2 – Mahmoud Saleh Apr 01 '15 at 13:04
  • Why don't you just transmit the saved changes via the flash scope, to the search bean? Your current approach is counting on a situation where the user won't have more than one window/tab open for that particular page. Not that it would matter anyway: a viewscope exists within the current `FacesContext`. You can't reach across to a different context to get a viewscoped bean that exists there. – kolossus Apr 01 '15 at 17:48
  • @kolossus, can you please an example of how to accomplish that with flash scope or at least tutorial link for similar solution (i need a place to start from), i never used it before. – Mahmoud Saleh Apr 02 '15 at 10:02
  • [Try this](http://jugojava.blogspot.com/2011/06/jsf2-flash-scope-example.html). Unfortunately, I'm not too impressed with the flash scope tutorials out there (and Bauke hasn't written one). If you have problems implementing a solution using this link, update your question with your working code and anyone here will be able to help – kolossus Apr 08 '15 at 01:37

0 Answers0