在我的应用程序中,我使用 primefaces 可编辑数据表,这些 List(cmFieldsList) 值需要根据请求参数(blkcode)动态呈现。
如果我在构造函数中初始化列表,我可以编辑和保存数据表对象。对于我的应用程序,每次都需要基于请求参数的动态列表,所以如果没有在构造函数中初始化,我就无法在我的数据表中编辑或保存数据。如果我尝试在不初始化的情况下编辑相同的内容,我会收到如下所示的异常
原因:java.lang.IllegalArgumentException: org.primefaces 的 javax.faces.component.UIData.getRowData(UIData.java:433) 的 javax.faces.model.ListDataModel.getRowData(ListDataModel.java:69) 的行不可用.component.datatable.DataTable.queueEvent(DataTable.java:522) at org.primefaces.component.behavior.ajax.AjaxBehaviorRenderer.decode(AjaxBehaviorRenderer.java:42) at javax.faces.component.behavior.ClientBehaviorBase.decode(ClientBehaviorBase .java:64) 在 org.primefaces.renderkit.CoreRenderer.decodeBehaviors(CoreRenderer.java:329) 在 org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:57)
所以如果没有在构造函数中初始化,我怎么能做到这一点。
我的 JSF 托管 Bean 代码是
package com.dynamic.mbeans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.RowEditEvent;
import com.dynamic.model.CmFields;
import com.dynamic.services.CmFieldService;
@ManagedBean(name="cmFieldListBean")
@RequestScoped
public class CmFieldController implements Serializable {
private static final long serialVersionUID = 6250736369064467010L;
private CmFields cmFields;
private List<CmFields> cmFieldsList;
@ManagedProperty(value="#{param.blkCode}")
public String blkCode;
public CmFieldController(){
cmFields= new CmFields();
//cmFieldsList= new ArrayList<CmFields>();
//cmFieldsList.add(cmFields);
}
@ManagedProperty("#{cmFieldService}")
private CmFieldService cmFieldService;
public String showForms(){
System.out.println("blkCode =="+blkCode);
cmFieldsList = cmFieldService.listAllCmField(blkCode);//lists CmFields from DB
return "dynamic-cmfields-form";
}
public void onEdit(RowEditEvent event) {
System.out.println("event started");
System.out.println("event ended");
// FacesMessage msg = new FacesMessage("CmFields Edited", ((CmFields) event.getObject()).getBlkCode() );
//FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("CmFields Cancelled", ((CmFields) event.getObject()).getBlkCode());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public CmFields getCmFields() {
return cmFields;
}
public void setCmFields(CmFields cmFields) {
this.cmFields = cmFields;
}
public CmFieldService getCmFieldService() {
return cmFieldService;
}
public void setCmFieldService(CmFieldService cmFieldService) {
this.cmFieldService = cmFieldService;
}
public List<CmFields> getCmFieldsList() {
return cmFieldsList;
}
public void setCmFieldsList(List<CmFields> cmFieldsList) {
this.cmFieldsList = cmFieldsList;
}
public String getBlkCode() {
return blkCode;
}
public void setBlkCode(String blkCode) {
this.blkCode = blkCode;
}
}
我用于向托管 bean 发送请求参数的 JSF 代码是
<h:form>
<h:commandLink action="#{cmFieldListBean.showForms}" value="LIST SALESMAN" >
<f:param name="blkCode" value="SALESMAN" />
</h:commandLink>
<br/>
<br/>
<h:commandLink action="#{cmFieldListBean.showForms}" value="LIST SALESMAN COMPANY" >
<f:param name="blkCode" value="SALESMAN_COMP" />
</h:commandLink>
</h:form>
我在数据表中列出的 JSF 代码是
<h:form id="form">
<p:growl id="messages" showDetail="true"/>
<p:dataTable var="cmList" value="#{cmFieldListBean.cmFieldsList}" id="cmFieldsList" editable="true" preserveDataModel="false">
<f:facet name="header">
Grid Editing
</f:facet>
<p:ajax event="rowEdit" listener="#{cmFieldListBean.onEdit}" update=":form:cmFieldsList" />
<p:ajax event="rowEditCancel" listener="#{cmFieldListBean.onCancel}" update=":form:cmFieldsList" />
<p:column headerText="Caption" style="width:200px">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{cmList.fieldCaption}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{cmList.fieldCaption}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Field Name" style="width:150px">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{cmList.fieldName}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{cmList.fieldName}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Data Type" style="width:150px">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{cmList.dataType}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{cmList.dataType}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Options" style="width:50px">
<p:rowEditor />
</p:column>
</p:dataTable>