我使用<ui:include>
加载数据表(我使用Primefaces)。我想使用listener
中的<ui:param>
到标签<p:ajax>
中。我测试了关闭的代码,但没有触发事件onRowEdit或onRowCancel。这是我的页面:
...
<ui:include src="../Componentes/tablaEditable.xhtml">
<ui:param name="columnas" value="#{tabla2FuentesHL7.dataTableColumns}" />
<ui:param name="bean" value="#{tabla2FuentesHL7.listTabla2FuenteDTO}" />
<ui:param name="aceptarEdicion" value="#{tabla2FuentesHL7.onRowEdit}" />
<ui:param name="cancelarEdicion" value="#{tabla2FuentesHL7.onRowCancel}" />
</ui:include>
...
我的数据表:
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:form >
<p:dataTable value="#{bean}" scrollable="true" scrollHeight="100" var="dato" editable="true">
<p:ajax event="rowEdit" listener="#{aceptarEdicion}" />
<p:ajax event="rowEditCancel" listener="#{cancelarEdicion}" />
<c:forEach items="#{columnas}" var="column" varStatus="loop">
<p:column headerText="#{column.header}" sortBy="#{dato[column.property]}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{dato[column.property]}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{dato[column.property]}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
</c:forEach>
<p:column style="width:32px">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
我的豆子:
@ManagedBean(name = "tabla2FuentesHL7")
@ViewScoped
enter code herepublic class Tabla2FuentesHL7 {
private static final long serialVersionUID = 1L;
private List<DataTableColumn> dataTableColumns = new ArrayList<DataTableColumn>();
private List<Tabla2FuenteDTO> listTabla2FuenteDTO = new ArrayList<Tabla2FuenteDTO>();
@PostConstruct
public void init() {
listTabla2FuenteDTO = Test.getFuenteTabla2();
dataTableColumns = CargarTablas.getTabla2FuenteHL7();
}
public List<Tabla2FuenteDTO> getListTabla2FuenteDTO() {
return listTabla2FuenteDTO;
}
public List<DataTableColumn> getDataTableColumns() {
return dataTableColumns;
}
public void onRowEdit(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Elemento modificado");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Modificación cancelada");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
if (newValue != null && !newValue.equals(oldValue)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
这就是错误:
javax.el.PropertyNotFoundException: /Componentes/tablaEditable.xhtml @12,76 listener="#{cancelarEdicion}": /Modulos/hl7.xhtml @31,104 value="#{tabla2FuentesHL7.onRowCancel}": The class 'com.queres.xedoc.consola.componentes.Tabla2FuentesHL7' does not have the property 'onRowCancel'.
如果我改变这个:
<p:ajax event="rowEdit" listener="#{aceptarEdicion}" />
<p:ajax event="rowEditCancel" listener="#{cancelarEdicion}" />
为此:
<p:ajax event="rowEdit" listener="#{tabla2FuentesHL7.onRowEdit}" />
<p:ajax event="rowEditCancel" listener="#{tabla2FuentesHL7.onRowCancel}" />
我的代码运行良好,但我需要一个动态数据表。有什么方法可以将参数传递给侦听器吗?谢谢
<ui:param>
只能传递值表达式,而不能传递方法表达式。
更好地利用EL通过大括号[]
表示法参数化方法名称的能力。然后,您可以将方法名称声明为普通的String
。
<ui:include src="../Componentes/tablaEditable.xhtml">
...
<ui:param name="beanEdicion" value="#{tabla2FuentesHL7}" />
<ui:param name="aceptarEdicion" value="onRowEdit" />
<ui:param name="cancelarEdicion" value="onRowCancel" />
</ui:include>
<p:ajax event="rowEdit" listener="#{beanEdicion[aceptarEdicion]()}" />
<p:ajax event="rowEditCancel" listener="#{beanEdicion[cancelarEdicion]()}" />
更新根据评论,它似乎在PrimeFaces 5.1中仍然不起作用,尽管相关问题表明它已经在3.5中修复。你基本上需要重新讨论这个问题。
同时,您可以借助JSF实用程序库OmniFaces的<o:methodParam>
标签来解决此问题。这基本上将值表达式转换为方法表达式:
<ui:include src="../Componentes/tablaEditable.xhtml">
...
<ui:param name="aceptarEdicion" value="#{tabla2FuentesHL7.onRowEdit}" />
<ui:param name="cancelarEdicion" value="#{tabla2FuentesHL7.onRowCancel}" />
</ui:include>
<o:methodParam name="aceptarEdicionParam" value="#{aceptarEdicion}" />
<o:methodParam name="cancelarEdicionParam" value="#{cancelarEdicion}" />
...
<p:ajax event="rowEdit" listener="#{aceptarEdicionParam}" />
<p:ajax event="rowEditCancel" listener="#{cancelarEdicionParam}" />