JSF OneMenu
Hi...
I user dropDownMenu like this:
<h:selectOneMenu onchange="javascript:changePlanet()" id="changePlanet" value="#{loginAction.planet}">
<f:selectItems id="allPlanets" value="#{loginAction.allPlanets}"/>
</h:selectOneMenu>
I have collection of few planets like :
Mars,
Earth,
Pluton etc.
when i change planet on menu, ajax is firered and change planet on ma field
private String currentPlanet, but my problem is when i change planet and go back to this page using outputLink, page is recompiled and here is my problem. Always have first element in selectOneMenu component? Answer is must select thos element but how?
can anyone help me how to select those element?
Maybe i should use binding?
# 1
As long as loginAction.planet (method getPlanet, setPlanet) is set to a value in the list before the page is rendered, you should get that item in the list selected.
The loginAction bean will need to be session scope for that to work properly though.
Hehe, oh and Pluto is no longer a valid planet! =D
CowKing
# 2
But I'm new in JSF, and Can't do this....could U tel me how to change "selected" attribute in my planets from server side?
I have collection on SelectItem and planet String.
private String planet;
private List<SelectItem> allPlanets;
What should I do to change "Selected" attribute?
could U give me some example...
Regards!
# 4
> But I'm new in JSF, and Can't do this....could U tel
> me how to change "selected" attribute in my planets
> from server side?
>
> I have collection on SelectItem and planet String.
> > private String planet;
> private List<SelectItem> allPlanets;
>
>
> What should I do to change "Selected" attribute?
Set it in the String planet.
# 5
I do somethink like this...
First I get ID of selected Item in Javascript and sending to server via AJAX...sample ID=4
idPlanet = 4;
this.planet.setPlanet(allPlanets.get(idPlanet).getPlanetName());
and this doesn't works to...
loginAction komponent is session scope...
# 6
In your loginAction bean, do you have accessors called getPlanet and setPlanet? You have tied your selectOneMenu components value attribute to these methods. These methods will get or set the selected item from your list.
Secondly, you will have a getAllPlanets method (also in your loginAction bean) that returns a List of SelectItem objects. A SelectItem object consists of a label and an id.
The labels in the SelectItem object are used to display the menu options. When a user selects an item from your list and clicks submit (or you force a submit onchange via JavaScript) the id of the SelectItem that was selected is transferred to the value of the menu (i.e. setPlanet method is called with the id selected).
Therefore, if you want a specific item pre-selected in your menu component, you have to call setPlanet and give it the SelectItem id of the item to be selected. Usually this is done in your bean constructor.
Or, if you want the item selected by the user to remain selected after the page is submitted, you should NOT change the value of setPlanet.
I don't know a lot about AJAX, but you may be interfering with the planet value.
Lastly, loginAction is a JSF managed-bean, not a component. I'm pretty sure you understand this, but it is important that you are clear. The loginAction managed-bean MUST be set to session scope in your faces-config.xml file.
Hope this clears things up a bit,
CowKing