Simple question on a form...
Hi guys,
i need your help!!!!!!!!!!!!!!!!
I've read JSF Sun documentation and JSF in Actions but i don't find solution to my problem.
I've to develop a form that ask to user to insert a username and a role between user and administrator.
There is a go button that check if the user with that username and role exists in a database and performs navigation.
I want to use selectOneMenu for showing to user the two possibilities, user and administrator.
Here is my form
<h:form>
<h:panelGrid columns="2" border="0" cellpadding="3" cellspacing="3">
<f:facet name="header">
<h:outputText value="Welcome to MicroArray Application" />
</f:facet>
<h:outputText value="Enter your user name:"/>
<h:inputText value="#{authenticationBean.loginName}">
<f:validateLength minimum="5" maximum="30"/>
</h:inputText>
<h:outputText value="Enter your role:"/>
?
<h:commandButton action="#{authenticationBean.login}" value="Login"/>
</h:form>
Van you help me to complete my code relative to role inserting?
I want a selection between user and administrator with selectOneMenu.
Can you help me with code?
Thanks very much,
i need your help,please
[1686 byte] By [
giubata] at [2007-10-3 2:24:41]

JSF<h:selectOneMenu value="#{myBean.selectedItem}">
<f:selectItems value="#{myBean.selectItems}" />
</h:selectOneMenu>
MyBeanprivate String selectedItem;
public String getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(String selectedItem) {
this.selectedItem = selectedItem;
}
public List getSelectItems() {
List selectItems = new ArrayList();
selectItems.add(new SelectItem("key1", "value1"));
selectItems.add(new SelectItem("key2", "value2"));
selectItems.add(new SelectItem("key3", "value3"));
return selectItems;
}
String selectedItem should contain the "key" when selected.
Suggestion is to prepare the items in a LinkedHashMap(). And in the getSelectItems you can iterate through the keySet() of the LinkedHashMap and put each key and value in a new SelectItem(). Then in the retrieval of the selected item you just do a LinkedHashMap.get("key"); to get the value.
thanks very much, but can you help me to complete my code?
I've understood your code but i'm not able to develop the second part.......
P.S. What have i use as key1, key2,key3?
In my problem i've user and administrator....how can i complete my code?
Please help me,i know it's too easy for you but it's very important for me......i've lots of selectOneMenu in my application and i need to know how solve a complete example.....
if you have no time to help me can you post my some link with a complete example?
Please help me.......
Key is the key .. The value is the value as shown in the listbox.
The HTML is as follows:<option value="key">value</option>
In your case, do for examplenew SelectItem("1", "Administrator");
new SelectItem("2", "User");
which generates<option value="1">Administrator</option>
<option value="2">User</option>
The selectedItem returns 1 if Administrator is selected and 2 if User is selected. Do it your way.
thanks very much BalusC!!!!!
You've solved my datatables problems and now you've helped me to solve my problem!!!!!!
Thanks!!!!!!
I need yet a little help.
I've understood how generate a drop-list with fixed value(with your help) in a form.
Now suppose i want this list(user/administrator) comes from a query
"select group from mytable"
How have i modify your old code?
I want the same form but i want the list is generated dinamically by a mysql table....
please help me..
THANKS
Well, loop through the results, get the key and value and add it as a new selectItem to the List selectItems.
thanks..
help me with this code.
I hope this is the right function, that returns a list of selectItems objects...
My table researchteam has an only attribute, teamname.
Can you help me to correct my code?
private List<SelectItem> teams;
public List<SelectItem> getTeams() throws SQLException {//teamList()
try{
DataSource dataSource=Singleton.getInstance().getDataSource();
Connection conn=dataSource.getConnection();
if (conn != null) {
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery("select * from researchteam");
List<SelectItem> teams = new ArrayList<SelectItem>();
while (rst.next())
teams.add(new SelectItem(rst.getString("teamname"),rst.getString("teamname")));
rst.close();
conn.close();
}
}
catch(Exception e){};
return teams;
}
with this code and
<h:selectOneMenu value="#{authenticationBean.teamName}">
<f:selectItems value="#{authenticationBean.teams}" />
</h:selectOneMenu
>
i've an error of empty list or collection....
what's wrong?
Please help me,i need a little help and i will solve all my problems...
i'm not sure about
while (rst.next())
teams.add(new SelectItem(rst.getString("teamname"),rst.getString("teamname")));
can you help me correcting the code?
Thanks very much
Hey,
i'm not sure, but i think i had the same problem. The thing i'm suggesting is to change the type of the "get" method and the field from List<SelectItem>
(cuz it's abstract as long as i remeber) to ArrayList<SelectItem>
I guess the abstract method "add" does not work...
Regards,IL
p.s. I'm not claiming to be right but i think i had this problem. The way to check it is to debug. I'd like to do the check but i don't have time for it now:(. Nevertheless a change like this won't cost much effort and i still think this may solve the problem.
Message was edited by: IL
il4etobg
Do you actually get the data? Just do some debugging.Edit: List indeed is abstract, but the add actually refers to ArrayList.Message was edited by: BalusC
I've this error
javax.servlet.ServletException: javax.servlet.jsp.JspException: Value binding '#{authenticationBean.teams}'of UISelectItems with component-path {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /Login.jsp][Class: javax.faces.component.html.HtmlForm,Id: _id1][Class: javax.faces.component.html.HtmlPanelGrid,Id: _id2][Class: javax.faces.component.html.HtmlPanelGrid,Id: _id4][Class: javax.faces.component.html.HtmlSelectOneMenu,Id: _id11][Class: javax.faces.component.UISelectItems,Id: _id12]} does not reference an Object of type SelectItem, SelectItem[], Collection or Map but of type : null
It's two weeks i try to solve it, but i don't understand what is the problem.
In my Login.jsp i've done
<h:selectOneMenu value="#{authenticationBean.teamName}">
<f:selectItems value="#{authenticationBean.teams}" />
</h:selectOneMenu>
and in my authenticationBean i've
private ArrayList<SelectItem> teams;
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public ArrayList<SelectItem> getTeams() throws SQLException {//teamList()
try{
DataSource dataSource=Singleton.getInstance().getDataSource();
Connection conn=dataSource.getConnection();
if (conn != null) {
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery("select * from researchteam");
ArrayList<SelectItem> teams = new ArrayList<SelectItem>();
while (rst.next())
teams.add(new SelectItem(rst.getString("teamname"),rst.getString("teamname")));
rst.close();
conn.close();
}
}
catch(Exception e){};
return teams;
}
i'm not able to solve my problem,
can someone help me?
Please, help me i'm a new user of Jsf..
thanks very much for your help,............
I'VE SOLVED MY PROBLEM!!!!!!!!
I hope i can help someone with my solution....
here is my authenticationBean
private List<SelectItem> teams;
public List<SelectItem> getTeams() throws SQLException {//teamList()
List<SelectItem> teams = new ArrayList<SelectItem>();
try{
DataSource dataSource=Singleton.getInstance().getDataSource();
Connection conn=dataSource.getConnection();
if (conn != null) {
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery("select * from researchteam");
//ArrayList<SelectItem> teams = new ArrayList<SelectItem>();
while (rst.next())
teams.add(new SelectItem(rst.getString("teamname"),rst.getString("teamname")));
rst.close();
conn.close();
}
//return teams;
}
catch(SQLException e){};
return teams;
}
and this is my jsp page
<h:selectOneMenu value="#{authenticationBean.teamName}">
<f:selectItems value="#{authenticationBean.teams}" />
</h:selectOneMenu>
good luck!
I having problem, while retriving the selected items, as my bean scope is request, i don't want to keep in session, so when i selected the value from List , I am able to get the selected value, but again its checking my List, its empty at that time, I don't know why it has to see again for the list.,
any suggestion retriving the values from selcted list, using request scope.
I am getting this error, becuase my list is empty
java.util.NoSuchElementException
javax.faces.component.SelectItemsIterator.next(SelectItemsIterator.java:98)
javax.faces.component.SelectItemsIterator.next(SelectItemsIterator.java:124)
javax.faces.component.UISelectOne.matchValue(UISelectOne.java:141)
javax.faces.component.UISelectOne.validateValue(UISelectOne.java:114)
javax.faces.component.UIInput.validate(UIInput.java:645)
javax.faces.component.UIInput.executeValidate(UIInput.java:849)
javax.faces.component.UIInput.processValidators(UIInput.java:412)
javax.faces.component.UIForm.processValidators(UIForm.java:170)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:912)
javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:342)
com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:78)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
Thanks
Srini
Message was edited by:
srikanthg
When solving problems like this, look for empty catch blocks like the one you have in your code.If you had just a e.printStackTrace() in there, I am certain you would have found your problem much sooner.