0

I am trying to make a drop down box, which gets choices from a database held on a Glassfish server. I have managed to achieve this. The next step was to have the choices from the user appear on the next page. I have attempted to do this by doing this

index.xhtml

<h:selectOneMenu value="#{mealBean.monLunch}">
    <f:selectItems value="#{mealBean.getMealsByTime('Lunch')}"/>
</h:selectOneMenu>

There is a command button linking to the results.xhtml from index.xhtml as follows

Bean

public String monLunch;
public String monDinner;

public String getMonLunch() {
    return monLunch;
}

public void setMonLunch(String monLunch) {
    this.monLunch = monLunch;
}
public void setMonDinner(String monDinner) {
    this.monDinner = monDinner;
}
public String getMonDinner() {
    return monDinner;
}

and then on the results.xhtml

#{mealBean.monLunch}

My issue is, when I click the command button, the data does not appear on the new page. Is there a way to remedy this? Any help would be appreciated. Thank you in advanced

  • Hi. I do not understand why you set monLunch in your xhtml and in your results.xhtml you try to get the monDinner... – Val Bonn Jan 02 '17 at 12:36
  • oh sorry, i've corrected it. The results.xhtml tries to display both the choices but i copied the wrong one haha – underSizedGiant Jan 02 '17 at 12:38
  • If you look at the code generated for your index.xhtml, are the "value" attributes OK in the "option" tags ? Do you have something like ? – Val Bonn Jan 02 '17 at 13:04
  • Yeah, all of the options appear in the drop down boxes from the database. They appear on the webpage and when i check the elements in the F12 menu – underSizedGiant Jan 02 '17 at 13:06
  • What is important is not what you see in the webpage, but the values of the "value" attributes in the option tags. What are the annotations on your bean ? What scope do you use ? – Val Bonn Jan 02 '17 at 13:12
  • Just seen your edit, yeah mine appears that way, it says – underSizedGiant Jan 02 '17 at 13:13
  • Annotations are @Named(value = "mealBean") and @Dependent – underSizedGiant Jan 02 '17 at 13:27
  • scope is @SessionScoped – underSizedGiant Jan 02 '17 at 13:29
  • Ok. I think you should try to remove the @Dependent, because it is in conflict with the SessionScoped : http://docs.oracle.com/javaee/6/tutorial/doc/gjbbk.html – Val Bonn Jan 02 '17 at 14:08
  • Okay, I removed the '@Dependant' and then when i tried to load the webpage again, the – underSizedGiant Jan 02 '17 at 14:15
  • Hmmmm... :( I do not understand why you need the dependent annotation. What does return your getMealsByTime ? A List ? What are your imports in the bean ? Can you try to replace Named(value = "mealBean") Dependent SessionScoped by ManagedBean(name = "mealBean") SessionScoped ? – Val Bonn Jan 02 '17 at 14:31
  • That actually fixed it haha changing the '@Named' to '@ManagedBean' With the '@SessionScoped'. Thanks for the help! – underSizedGiant Jan 02 '17 at 14:35
  • Good. Then I will write an "Answer" to your question :) – Val Bonn Jan 02 '17 at 14:36
  • This information needs to be in the question, otherwise people looking at this answer will not understand your @ValBonn's answer unless they read all the comments. – Esteban Rincon Jan 02 '17 at 14:57

1 Answers1

1

You should annotate your bean with @ManagedBean(name = "mealBean") instead of @Named(value = "mealBean").

It is not a good practice to mix CDI annotations like javax.inject.Named with the JSF annotations, like javax.faces.bean.SessionScoped or javax.faces.bean.ManagedBean.

And this way you can delete the useless @Dependent.

Val Bonn
  • 1,129
  • 1
  • 13
  • 31