我正在尝试通过选择一个菜单创建一个jsf数据表,并从数据库中填充它,但在这里无法正常工作是我的JSF代码:
<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>
第1页代码:
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;
}
最后是userTable代码:
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;
}
}
}
那么怎么了?
谢谢
最佳答案
这里,
<ice:selectOneMenu id="users" value="#{Page1.selectedUser}" >
您将使用单个托管bean属性而不是迭代行对象
#{users}
的属性来绑定输入值。因此,当表有多行时,单个托管bean属性将每次被覆盖,直到最后一行。该属性最终始终是最后一行的值。您需要将输入值绑定到迭代的行对象。和这里,
<f:selectItems value="#{users.screenName}" />
f:selectItems
应该绑定到List<SelectItem>
或SelectItem[]
或Map<Object, Object>
。我不确定Option[]
应该代表什么,但是看起来像ICEFaces特定。由于您没有描述问题(就像从最终用户的角度,而不是从开发人员的角度,您只是说“它不起作用”),所以我只是为了安全起见,建议您使用List<SelectItem>
。这是一个基本的启动示例,彼此之间的外观类似。我在这里使用了不明原因的变量名,以便使代码更清晰,更易于记录:
<h:dataTable value="#{bean.users}" var="user">
<h:column>
<h:selectOneMenu value="#{user.selectedScreenName}">
<f:selectItems value="#{user.availableScreenNames}" />
</h:selectOneMenu>
</h:column>
</h:dataTable>
与
public class Bean {
private List<User> users;
// ...
}
和
public class User {
private String selectedScreenName;
private List<SelectItem> availableScreenNames;
public User() {
availableScreenNames = new ArrayList<SelectItem>();
for (String availableScreenName: session.createQuery(SQL).list()) {
availableScreenNames.add(new SelectItem(availableScreenName));
}
}
// ...
}
就是说,我不确定“ booleancheckbox”(正如您在标题中所输入的)与该问题有何关系。将来,请尝试在问题中更具描述性。告诉“它不起作用”并不能给我们太多帮助,只能根据给定的代码进行猜测。