Hello.. I'm trying to create a jsf datatable with select one menu and to fill it from database but can't get it work here is my JSF code:
<ice:dataTable id="dataTable1" value="#{Page1.users}" var="users">
<ice:column id="column2">
<ice:outputText id="userID" value="users.userId"/>
<f:facet name="header">
<ice:outputText id="outputText1" value="Table_Column1"/>
</f:facet>
</ice:column>
<ice:column id="column2">
<f:facet name="header">
<ice:outputText id="outputText4" value="Table_Column2"/>
</f:facet>
<ice:selectOneMenu id="users" value="#{Page1.selectedUser}" >
<f:selectItems value="#{users.screenName}"/>
</ice:selectOneMenu>
</ice:column>
</ice:dataTable>
Page1 code:
private ArrayList<UserTable> users=new ArrayList<UserTable>();
public ArrayList<UserTable> getUsers() {
if(users==null){
UserTable u1=new UserTable(1);
users.add(u1);
u1=new UserTable(2);
users.add(u1);
}
return users;
}
private String selectedUser;
public String getSelectedUser() {
return selectedUser;
}
public void setSelectedUser(String selectedUser) {
this.selectedUser = selectedUser;
}
and Finally userTable code:
public class UserTable {
private long userId;
private Option[] screenName;
public UserTable(long userId) {
this.userId = userId;
}
public Option[] getScreenName() {
if(screenName==null){
buildScreenNameOptions();
}
return screenName;
}
public void setScreenName(Option[] screenName) {
this.screenName = screenName;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
private void buildScreenNameOptions() {
List<String> screenNameList = null;
try{
Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Query q = session.createQuery("Select screenName from User");
screenNameList = (List<String>) q.list();
} catch(Exception e) {
e.printStackTrace();
}
screenName = new Option[screenNameList.size()];
int i=0;
for(String name : screenNameList) {
Option opt = new Option(name);
screenName[i++] = opt;
}
}
}
So what's going wrong?
Thanks