如何通过< ui:param>传递bean动作/监听器方法的< ui:include> [英] How to pass bean action/listener method via <ui:param> of <ui:include>

查看:151
本文介绍了如何通过< ui:param>传递bean动作/监听器方法的< ui:include>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用<ui:include>加载数据表(我正在使用Primefaces).我想在标签<p:ajax>中使用listener中的<ui:param>.我测试了已关闭的代码,但未触发 onRowEdit onRowCancel 事件. 这是我的页面:

I'm using <ui:include> to load a data table (I'm using Primefaces). I want use <ui:param> in the listener into the tag <p:ajax>. I tested the code that is down, but not trigger the event onRowEdit or onRowCancel. This is my page:

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

我的代码工作正常,但是我需要一个动态数据表.有什么方法可以将参数传递给侦听器? 谢谢!

my code works fine, but I need a dynamic data table. Is there any way to pass a parameter to the listener? Thanks!

推荐答案

<ui:param>只能传递值表达式,而不能传递方法表达式.

The <ui:param> can only pass value expressions, not method expressions.

更好地利用EL通过大括号[]表示法对方法名称进行参数化的能力.然后,您可以将方法名称声明为普通香草String.

Better make use of the ability of EL to parameterize method names via brace [] notation. Then you can just declare the method names as plain vanilla 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]()}"  />


更新,根据注释,即使


Update as per the comments, it appears to still not work in PrimeFaces 5.1 even though the related issue says that it's already fixed in 3.5. You'd basically need to reopen the issue.

同时,您可以借助JSF实用程序库 OmniFaces .基本上,这会将值表达式转换为方法表达式:

In the meanwhile, you can workaround this with help of <o:methodParam> tag of JSF utility library OmniFaces. This basically converts a value expression to a method expression:

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

这篇关于如何通过&lt; ui:param&gt;传递bean动作/监听器方法的&lt; ui:include&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆