我正面臨着JSF和Datatables的一些麻煩。在我的bean中,我有一個依靠Datatable顯示在視圖中的對象列表。該對象非常簡單,每個都由一個整數(id)和一個字符串( 評論)組成。JSF Datatable:通過單個命令按鈕提交多行
最終目標是將對象的完整列表打印到用戶,同時將編輯一行或多行(通過h:inputtext字段),然後通過按鈕提交其更改。這就是問題所在。我已經使用「成功」數據表,通過依靠每行上的編輯/保存按鈕來編輯每行一行,但在這種情況下不適用,因爲最終用戶將面對多於500行的總計。
這裏是我使用(簡化的和萃取)
豆的代碼:
@ManagedBean
@RequestedScope
public class VlanBean {
private DataModel<VlanWrapper> model;
private List<VlanWrapper> vlans = new ArrayList<VlanWrapper>();
public VlanBean() {
vlans.add(new VlanWrapper(1, ""));
vlans.add(new VlanWrapper(2, "Prova2-2"));
vlans.add(new VlanWrapper(3, ""));
vlans.add(new VlanWrapper(4, ""));
vlans.add(new VlanWrapper(5, ""));
vlans.add(new VlanWrapper(6, "Prova3"));
vlans.add(new VlanWrapper(7, ""));
vlans.add(new VlanWrapper(8, ""));
}
public void commitChanges()
{
System.out.println("Before Model ");
for (VlanWrapper vw : model)
{
System.out.println("ModelOF " + vw.getId() + " - " + vw.getProject() + " - changed: " + vw.changed + ";");
}
}
public DataModel<VlanWrapper> getModel() {
if (model == null)
model = new ListDataModel<VlanWrapper>(vlans);
return model;
}
public void setModel(DataModel<VlanWrapper> model) {
this.model = model;
}
public class VlanWrapper
{
private boolean changed;
private int id;
private String project;
public VlanWrapper(int id, String prj)
{
this.id = id;
this.project = prj;
changed = false;
}
public String getProject() {
return project;
}
public void setProject(String project) {
System.out.println("Setting PRJ..");
changed = true;
this.project = project;
}
public int getId() {
return id;
}
public boolean isChanged() {
return changed;
}
}
}
檢視:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
</h:head>
<h:body>
<f:view>
<h:panelGroup>
<h:form>
<h:commandButton value="Commit changes to DB" action="#{VlanBean.commitChanges}">
</h:commandButton>
</h:form>
<h:dataTable value="#{VlanBean.model}" var="vlans">
<h:column><f:facet name="header">ID</f:facet>#{vlans.id}</h:column>
<h:column><f:facet name="header">Project</f:facet>
<h:inputText value="#{vlans.project}" immediate="true"/>
</h:column>
</h:dataTable>
</h:panelGroup>
</f:view>
</h:body>
</html>
的問題是,所有的變化我的視圖做沒有反映在豆,我還沒有找到任何有用的提示/張貼周圍(一些建議阿賈克斯強迫更新,其他的JavaScript,但我沒有讓他們中的任何工作,沒有人是真的關閉我希望使用它)。
我也嘗試去爲ui:重複,但我有同樣的行爲。任何想法/建議?我沒有義務使用Datatable,所以我打開其他解決方案/方法!
謝謝advace, 阿爾貝託
非常感謝您的回答,它爲我澄清了幾點!實際上我需要實現的流程我不需要編輯屬性,但它是一個非常好的命題和示例!尤其是ajax部分! – kappaalby