-1

I'm confronted with the problem of passing a value from a bean to another bean via injection.

The program structure: A user searches for pizzas, the program generates PizzaObjects and add it to the list 'results'. Now I want to pass the list to the bean PizzaResult.

my ManagedBean looks like this:

@ManagedBean
@SessionScoped
public class PizzaSearch {
   // variables
   private List<PizzaObject> results = new ArrayList<PizzaObject>();

   // methods to search for different pizzas

   // methods to fill the list of PizzaObjects

   // getter - setter
   public List<PizzaObject> getResults() {
      return results;
   }

Code snippet of the other bean:

@ManagedBean
@SessionScoped
public class PizzaResult {

   // injection
   @ManagedProperty(value="#{pizzaSearch}")
   private PizzaSearch pizzaSearch;

   // variables
   private List<PizzaObject> results;

   @PostConstruct
   public void initResults() {
      results = pizzaSearch.getResults();               // results = empty
      int size = results.size();                        // size = 0
      this._chosenSize = new int[size];                 // chosenSize = 0
      this._chosenQuantity = new int[size];             // chosenQuantity = 0
   }

   public void addToCart(int index) {
      System.out.println("Parameter: " + results.get(index).getPizza().getPizzaID());
      System.out.println("chosen size: " + getChosenSize()[index]);
      System.out.println("chosen quantity: " + getChosenQuantity()[index]);
   }

   // getter and setter

I need to initialize the results in the PostConstruct but somehow each value is 0 there..

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3548416
  • 117
  • 1
  • 1
  • 11
  • By default a java.util.List is empty. If you create your `PizzaSearch` it is initiated with a new resp. empty ArrayList. Therefore the output is correct. Do you mind indicating where you expect the list to be filled and where the loading of the ? PizzaObject`s should happen? – Markus Aug 08 '14 at 12:19
  • By the way you used the ClassName `PizzaSearch` twice! – Markus Aug 08 '14 at 12:20
  • The doubled class name was a slip by translating and writing the code into this topic. PizzaSearch and PizzaResult should be correct. But exactly this was my question. Where should I initiate the result list if not in the PostConstructor? – user3548416 Aug 08 '14 at 12:43
  • Where are you filling the list in the first bean? I dare to say that it is just an empty list. So how do you expect the value to not be 0, of course it is 0. – Omoro Aug 08 '14 at 12:43
  • @Omoro look what I've written in the first snippet: "// methods to fill the list of PizzaObjects". There DEFINETELY is a method to fill the list and of course it is used.. But I don't know how to pass this list to the second bean. – user3548416 Aug 08 '14 at 12:47
  • Whatever you are doing there should affect the list variable `results` and that's why it is important for us to see how you are doing that fill so that we can help you. And you should use the injected managedbean variable to get the list `pizzaSearch.getResults()`. – Omoro Aug 08 '14 at 12:48
  • If you separate business logic (PizzaResult) and the data logic (PizzaSearch) you can write a "service". In the JSF Bean PizzaResult you query for the data the first time it is loaded and then store it in the session (if `results == null` load it). (==> There are a bunch of downsides like missing replication in Tomcat or misleading behavior if you manually delete an entry in the database.) – Markus Aug 08 '14 at 12:51

1 Answers1

0
  • To be clear can you specified the name of your ManagedBean like this @ManagedBean(name = "pizzaSearch") (evenif its done by default ).
  • check your importation for SessionScoped it should be import javax.faces.bean.SessionScoped; and not import javax.enterprise.context.SessionScoped; , see more informations about the differences between this here

Or you can use @Inject Annotation :

CDI-based bean definitions

javax.enterprise.context.SessionScoped //for bean scoping
javax.inject.Named //for bean declaration
javax.inject.Inject //for injection

So to inject your PizzaSearch managedBean into PizzaResult follow this :

in PizzaSearch

@Named
@SessionScoped
public class PizzaSearch {
// your code here

and for PizzaResult

@Named
@SessionScoped
public class PizzaResult {

@Inject
PizzaSearch pizzaSearch;
//code here

There are another way , if you used SessionScoped , so you can get your current instances of PizzaSearch by using the FacesContext

Community
  • 1
  • 1
Abdelghani Roussi
  • 2,707
  • 2
  • 21
  • 39