2

OminiFaces 的 'o:methodParam' 现在对我有用,如下所示。我该如何使用另一种方式?我不知道我在其中缺少什么。它可以使用<h:commandButton><a4j:jsFunction>不使用SeamSeam使用时它不使用<a4j:jsFunction>

发展环境是 RichFaces 4. Seam 2.3 OminiFaces 1.2 JBoss 7.1.1

@Name("DataTableBacking")
public class DataTableBacking {

    Department[] items = {new Department("AAA", "AAA"), new Department("BBB", "BBB"), new Department("CCC", "CCC")};

    public Department[] getItems() {
        return items;
    }

    public void action(Department action) {
        System.out.println("Action called with:" + action.getName());
    }

}

数据表.xhtml

<h:html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:richm="http://developmentTutorials.com/java">
        <h:body>
            <h:form>
            <h1>Data Table</h1>
                <rich:dataTable id="departmentTable" value="#{DataTableBacking.items}" var="dep" style="width:100%">
                    <rich:column style="width:100px;text-align:center;">
                        #{dep.name}
                        <richm:confirmLink actionBeanMethod="#{DataTableBacking.action(dep)}" render="departmentTable"/>
                    </rich:column>
                </rich:dataTable>
            </h:form>
        </h:body>
</h:html>

在标签库中,confirmation.xml

<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:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j" 
    xmlns:o="http://omnifaces.org/ui"
    xmlns:of="http://omnifaces.org/functions"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <o:methodParam name="methodParam" value="#{actionBeanMethod}" />

    <a4j:commandLink value="delete" onclick="#{rich:component('confirmation')}.show();return false" />

    <h:commandButton value="direct" action="#{methodParam}" />

    <a4j:jsFunction name="submit" action="#{methodParam}" render="#{render}" />

    <rich:popupPanel id="confirmation" width="250" height="150">
        <f:facet name="header">Confirmation</f:facet>
        <h:panelGrid>

            <h:panelGrid columns="1">
                <h:outputText value="Are you sure?" style="FONT-SIZE: large;" />
            </h:panelGrid>

            <h:panelGroup>
                <input type="button" value="OK" onclick="#{rich:component('confirmation')}.hide(); submit(); return false" />
                <input type="button" value="Cancel" onclick="#{rich:component('confirmation')}.hide(); return false" />
            </h:panelGroup>
        </h:panelGrid>
    </rich:popupPanel>

</ui:composition>
4

2 回答 2

2

我不完全确定它的null价值。如果回发后数据不存在(经常出现的错误),则根本不应该调用该方法。

不过,您的代码中确实有一个错误,这a4j:jsFunction将创建一个函数并将其分配给一个名为 的 js 变量submit,这对于每一行都是相同的。

所以submit=function(){RichFaces.ajax("j_idt15:departmentTable:0:j_idt18" ...对于第一行,submit=function(){RichFaces.ajax("j_idt15:departmentTable:1:j_idt18" ...对于第二行,依此类推。

然后,您的对话框将始终调用最后一行的对话框。那么,有可能,null您提供表格的列表中的最后一个值是否存在?

使用下面稍微简化的代码,我得到了 action 方法中传递的参数:

@ManagedBean
public class DataTableBacking {

    String[] items = {"A", "B"};

    public String[] getItems() {
        return items;
    }

    public void action(String action) {
        System.out.println("Action called with:" + action);
    }

}

数据表.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:richm="http://developmentTutorials.com/java"
>
    <h:head/>
    <h:body>

        <h:form>
            <rich:dataTable id="departmentTable" value="#{dataTableBacking.items}" var="dep" style="width:100%">
                <rich:column style="width:100px;text-align:center;">
                    #{dep}
                    <richm:confirmLink actionBeanMethod="#{dataTableBacking.action(dep)}" render="departmentTable"/>
                </rich:column>
            </rich:dataTable>
        </h:form>

    </h:body>
</html>

确认.xhtml:

<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:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j" 
    xmlns:o="http://omnifaces.org/ui"
    xmlns:of="http://omnifaces.org/functions"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <o:methodParam name="methodParam" value="#{actionBeanMethod}" />

    <a4j:commandLink value="delete" onclick="#{rich:component('confirmation')}.show();return false" />

    <h:commandButton value="direct" action="#{methodParam}" />

    <a4j:jsFunction name="submit" action="#{methodParam}" render="#{render}" />

    <rich:popupPanel id="confirmation" width="250" height="150">
        <f:facet name="header">Confirmation</f:facet>
        <h:panelGrid>

            <h:panelGrid columns="1">
                <h:outputText value="Are you sure?" style="FONT-SIZE: large;" />
            </h:panelGrid>

            <h:panelGroup>
                <input type="button" value="OK" onclick="#{rich:component('confirmation')}.hide(); submit(); return false" />
                <input type="button" value="Cancel" onclick="#{rich:component('confirmation')}.hide(); return false" />
            </h:panelGroup>
        </h:panelGrid>
    </rich:popupPanel>

</ui:composition>

我通过 a 添加了直接调用,h:commandButton以验证表达式确实正确地添加到 Facelet 标记中。在此示例中,当我单击链接并确认时,我得到了两行方法中delete预期的最后一个元素 (B) 。action()

我使用了 JBoss AS 7.1.1、OmniFaces 1.2 快照和 RichFaces 4.0.0。OmniFaces 版本无关紧要,因为我没有对methodParam这些版本之间进行任何更改(我是该特定部分的作者)。

您使用的是哪个服务器以及 OmniFaces 和 RichFaces 的哪个版本?

编辑

根据评论,将 更改StringDepartment

DataTableBacking.java:

@ManagedBean
public class DataTableBacking {

    Department[] items = {new Department(), new Department()};

    public Department[] getItems() {
        return items;
    }

    public void action(Department action) {
        System.out.println("Action called with:" + action);
    }

}

部门.java:

public class Department {

}

(所有其他代码与之前相同)

这应该并且(在我身边)确实没有任何区别。当你把String数组变成一个的Department时候,你是不是和我一样做的?你能展示你的全部后盾吗?

于 2012-09-19T22:45:33.497 回答
0

我尝试使用javascript. 它可能是不合适的解决方案。作为参数datatable传递row Index,因为我想<h:commandButton>为每一行数据表获取 id。<h:commandButton>当用户单击OKConfirmationBox 中的按钮时, JavaScript 将单击。

确认.xml

    <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:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j" 
    xmlns:o="http://omnifaces.org/ui"
    xmlns:of="http://omnifaces.org/functions"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <o:methodParam name="methodParam" value="#{actionBeanMethod}" />
    <a4j:commandLink value="#{value}" onclick="#{rich:component('confirmation')}.show();return false"/>

    <h:commandButton id="commandButton" value="direct" action="#{methodParam}" style="display:none;"/>

    <rich:popupPanel id="confirmation" width="250" height="150">
        <f:facet name="header">Confirmation</f:facet>
        <h:panelGrid>

            <h:panelGrid columns="1">
                <h:outputText value="Are you sure ?" style="FONT-SIZE: large;" />
            </h:panelGrid>

            <h:panelGroup>
                <input type="button" value="OK" onclick="document.getElementById('#{index}:commandButton').click();#{rich:component('confirmation')}.hide();return false;" />
                <input type="button" value="Cancel" onclick="#{rich:component('confirmation')}.hide(); return false" />
            </h:panelGroup>
        </h:panelGrid>
    </rich:popupPanel>

    </ui:composition>

在数据表中;

    <rich:dataTable id="departmentTable" value="#{DataTableBacking.items}" var="dep" style="width:100%" rowKeyVar="index">
        <rich:column style="width:100px;text-align:center;">
            <f:facet name="header">
                <h:outputText value="Name"/>
            </f:facet>
            #{dep.name}
            <richm:confirmLink value ="Delete" index="tableForm:departmentTable:#{index}" actionBeanMethod="#{DataTableBacking.action(dep)}" render="departmentTable"/>
        </rich:column>
    </rich:dataTable>
于 2012-09-20T09:40:39.200 回答