在监听器中将<ui:param>用于标记<p:ajax> </p:ajax> </ui:param>

时间:2015-03-17 16:40:23

标签: jsf parameter-passing facelets managed-bean uiinclude

我使用<ui:include>加载数据表(我使用Primefaces)。我希望<ui:param>中的listener使用标记<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}"  />

我的代码工作正常,但我需要一个动态数据表。有没有办法将参数传递给监听器? 谢谢!

1 个答案:

答案 0 :(得分:8)

<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]()}"  />

根据评论

更新,即使related issue表示已在3.5中修复,它仍然无法在PrimeFaces 5.1中运行。你基本上需要重新打开这个问题。

同时,您可以借助JSF实用程序库<o:methodParam>OmniFaces标记来解决此问题。这基本上将值表达式转换为方法表达式:

<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}"  />