Help with SelectOneMenu and SelectItems

Hi,

I am trying to get a selectone menu working.

I need to set the "selected" value into a beans variable and the options in the select box are in the form of a list ...

<h:selectOneMenu value="#{UserBean.treePath}">

<f:selectItems value="#{UserBean.treeList}"/>

</h:selectOneMenu>

How do i create an array of SelectItems[] while iterating through a list?

And after the user selects on option, how do i do some processing and redirect the user to a different page ?

Thanks in advance.

[557 byte] By [cs_jsfa] at [2007-11-27 6:57:53]
# 1

Well in the above case make sure the Managed Bean's (UserBean) property treePath holds the defualt value b4 rendering the SelectOneMenu Drop Down that you want as a defualt selected Item.If it holds nothing by defualt the first element in the List/Collection/SelectItem[] wud be selected.

<h:selectOneMenu value="#{UserBean.treePath}">

<f:selectItems value="#{UserBean.treeList}"/>

</h:selectOneMenu>

eg:

public class UserBean{

private List<SelectItem> treeList;

private String treePath;

public UserBean(){

treeList = new ArrayList<SelectItem>();

treeList.add(new SelectItem("value1","label1"));

treeList.add(new SelectItem("value2","label 2"));

treeList.add(new SelectItem("value3","label3"));

this.treePath= "value2";

_

_

}

public void setTreePath(String treePath){

this.treePath = treePath;

}

public String getTreePath(){

return this.treePath;

}

public void setTreeList(List<SelectItem> treeList){

this.treeList = treeList;

}

public List<SelectItem> getTreeList(){

return this.treeList;

}

}

Hope that might help :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 18:35:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> How do i create an array of SelectItems[] while

> iterating through a list?

You don't know how to create arrays? Here is a Sun Java Arrays tutorial: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

The f:selectItems also accepts a List by the way, if you find it easier.

> And after the user selects on option, how do i do

> some processing and redirect the user to a different

> page ?

The selected option is just available in the action method. Then you can use HttpServletResponse#sendRedirect() to invoke a redirect. The HttpServletResponse is available through the FacesContext by FacesContext.getCurrentInstance().getExternalContext().getResponse().

BalusCa at 2007-7-12 18:35:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Hi Rahul,Thanks for the detailed reply.I did almost the same thing by following another tutorial online.But this tells me that i did the right thing. I struggled with some null pointerexception. But now it is working.Thanks for the detailed reply.
cs_jsfa at 2007-7-12 18:35:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi Balu,

I think I did not put my Q clearly.

I was trying to create a SelectItem array and the length of the array was going to be dynamic. So i was unable to understand as to how to create it.

I then found another example where i can create a list of SelectItem objects and that worked for me. So you are right, the f:SelectItems with lists is what I used.

My doubt is:

<h:selectOneMenu value="#{UserBean.treePath}" >

<f:selectItems value="#{UserBean.selectItemObjListOfTrees}"/>

</h:selectOneMenu>

So to do some processing and writing the redirect, where can i write the code?

Do write it in the "biniding" attribute and give it a value of "#{UserBean.redirectUserFunction}"

You said it is available in the action, do i have to write something in the faces-config?

I have come to a stage where i can retrieve a list of values from the backing bean (as given in the code above), I now need to do some coding, validation and then redirect the user to another page.

Thanks for your help in advance.

cs_jsfa at 2007-7-12 18:35:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Also, when there is already a value in the database, is there a way that , that particicular value stays selected on the option menu?

Like in javascript and html we have the <select option "selected=true">

So in case i have a menu with the dates,

<select years>

<option value=2005>

<option value=2006>

<option value=2007>

<option value=2008>

</select>

So the year 2007 is the selected option, as it is the current year.

Is there a way where we can keep the selected option as the value to be shown ?So then if the user chooses some other value, then we can do the processing else, we dont have to do anything.

Thanks in advance.

cs_jsfa at 2007-7-12 18:35:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

> Also, when there is already a value in the database,

> is there a way that , that particicular value stays

> selected on the option menu?

>

> Like in javascript and html we have the <select

> option "selected=true">

> So in case i have a menu with the dates,

> <select years>

> <option value=2005>

> <option value=2006>

> <option value=2007>

> <option value=2008>

> </select>

>

> So the year 2007 is the selected option, as it is the

> current year.

> Is there a way where we can keep the selected option

> as the value to be shown ?So then if the user chooses

> some other value, then we can do the processing else,

> we dont have to do anything.

>

> Thanks in advance.

If the variable for the value attribute of the <h:selectOneMenu> tag has a value that is equal to the value of one of the selectItems, then that item will be the default for the menu. As for redirecting page access, you can write code in the bean that supplies the method for whatever component you use to submit the page. For instance, if you have <h:commandButton value="save" action="#{myBean.action}"/>

and in your MyBean class

public class MyBean{

.

.

.

public String action(){

if(SelectOneMenu_variable == choiceA){

//some code

return choice_A;

}

else if(SelectOneMenu_variable == choiceB){

//some code

return choice_B;

}

etc...

}

and in your faces-config.xml, you have navigation cases for each outcome that redirect the user to whatever page you wish.

Hope this helps

jco1323a at 2007-7-12 18:35:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...