Java 无法使用AJAX编辑表行

Java 无法使用AJAX编辑表行,java,jsf,jsf-2,Java,Jsf,Jsf 2,我有一个用于显示数据的JSF表。我想使用AJAX更新表中的行 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"

我有一个用于显示数据的JSF表。我想使用AJAX更新表中的行

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:ui="http://java.sun.com/jsf/facelets"
      >
    <h:head>
        <h:outputStylesheet library="css" name="table-style.css"  />
    </h:head>
    <h:body>

        <h1>JSF 2 dataTable example</h1>
        <h:form>
            <h:dataTable id="ajaxtable" 
                        value="#{order.orderModel}" var="o"
                styleClass="order-table"
                headerClass="order-table-header"
                rowClasses="order-table-odd-row,order-table-even-row">

                        <h:column>

                    <f:facet name="header">No</f:facet>

                    <h:inputText rendered="false"/>

                    <h:outputText value="#{order.orderModel.rowIndex + 1}"  />

                </h:column>
                <h:column>

                    <f:facet name="header">Order No</f:facet>

                    <h:inputText value="#{o.orderNo}" size="10" rendered="#{o.editable}" />

                    <h:outputText value="#{o.orderNo}" rendered="#{not o.editable}" />

                </h:column>

                <h:column>

                    <f:facet name="header">Product Name</f:facet>

                    <h:inputText value="#{o.productName}" size="20" rendered="#{o.editable}" />

                    <h:outputText value="#{o.productName}" rendered="#{not o.editable}" />

                </h:column>

                <h:column>

                    <f:facet name="header">Price</f:facet>

                    <h:inputText value="#{o.price}" size="10" rendered="#{o.editable}" />

                    <h:outputText value="#{o.price}" rendered="#{not o.editable}" />

                </h:column>

                <h:column>

                    <f:facet name="header">Quantity</f:facet>

                    <h:inputText value="#{o.qty}" size="5" rendered="#{o.editable}" />

                    <h:outputText value="#{o.qty}" rendered="#{not o.editable}" />

                </h:column>

                <h:column>

                    <f:facet name="header">Action</f:facet>

                    <h:commandLink id="editlink" value="Edit" action="#{order.editAction(o)}" rendered="#{not o.editable}">
                                    <f:ajax execute="@this" render="editlink"/>
                                </h:commandLink>

                </h:column>

            </h:dataTable>

            <h:commandButton value="Save Changes" action="#{order.saveAction}">
                    <f:ajax execute="@this" render="ajaxtable"/>
                </h:commandButton>


        </h:form>
    </h:body>

</html>

JSF2数据表示例
不
订单号
品名
价格
量
行动
按下此按钮(链接)时,表的行不会更新

    <h:commandLink id="editlink" value="Edit" action="#{order.editAction(o)}" rendered="#{not o.editable}">
          <f:ajax execute="@this" render="editlink"/>
    </h:commandLink>

我怀疑当我有链接时不能使用AJAX

第二个问题是:

            <h:column>

                <f:facet name="header">No</f:facet>

                <h:inputText rendered="false"/>

                <h:outputText value="#{order.orderModel.rowIndex + 1}"  />

            </h:column>
如果我删除
inputText
我会得到以下错误:
索引:0,大小:0
。我应该留下这个吗


最好的祝愿

问题中的ajax链接的
render
属性中存在错误:


您告诉它只渲染编辑链接本身。您的操作基本上与
render=“@this”
相同。您没有告诉它渲染表。您需要告诉它渲染表

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:ui="http://java.sun.com/jsf/facelets"
      >
    <h:head>
        <h:outputStylesheet library="css" name="table-style.css"  />
    </h:head>
    <h:body>

        <h1>JSF 2 dataTable example</h1>
        <h:form>
            <h:dataTable id="ajaxtable" 
                        value="#{order.orderModel}" var="o"
                styleClass="order-table"
                headerClass="order-table-header"
                rowClasses="order-table-odd-row,order-table-even-row">

                        <h:column>

                    <f:facet name="header">No</f:facet>

                    <h:inputText rendered="false"/>

                    <h:outputText value="#{order.orderModel.rowIndex + 1}"  />

                </h:column>
                <h:column>

                    <f:facet name="header">Order No</f:facet>

                    <h:inputText value="#{o.orderNo}" size="10" rendered="#{o.editable}" />

                    <h:outputText value="#{o.orderNo}" rendered="#{not o.editable}" />

                </h:column>

                <h:column>

                    <f:facet name="header">Product Name</f:facet>

                    <h:inputText value="#{o.productName}" size="20" rendered="#{o.editable}" />

                    <h:outputText value="#{o.productName}" rendered="#{not o.editable}" />

                </h:column>

                <h:column>

                    <f:facet name="header">Price</f:facet>

                    <h:inputText value="#{o.price}" size="10" rendered="#{o.editable}" />

                    <h:outputText value="#{o.price}" rendered="#{not o.editable}" />

                </h:column>

                <h:column>

                    <f:facet name="header">Quantity</f:facet>

                    <h:inputText value="#{o.qty}" size="5" rendered="#{o.editable}" />

                    <h:outputText value="#{o.qty}" rendered="#{not o.editable}" />

                </h:column>

                <h:column>

                    <f:facet name="header">Action</f:facet>

                    <h:commandLink id="editlink" value="Edit" action="#{order.editAction(o)}" rendered="#{not o.editable}">
                                    <f:ajax execute="@this" render="editlink"/>
                                </h:commandLink>

                </h:column>

            </h:dataTable>

            <h:commandButton value="Save Changes" action="#{order.saveAction}">
                    <f:ajax execute="@this" render="ajaxtable"/>
                </h:commandButton>


        </h:form>
    </h:body>

</html>
首先为
指定一个固定的
id
,以便稍后在
渲染中指定一个固定的客户端id


然后按如下方式修复ajax链接,以告知其呈现表:


另一种方法是将表绑定到视图,以便动态获取其客户端ID:



(这样,您也可以通过
{table.rowIndex}
显示行号,而不需要
数据模型

另见:

缺少一些东西。我这样编辑代码:什么也没发生。问题是JSF页面没有刷新。如果我点击编辑按钮,什么也不会发生。如果我从FF刷新按钮手动刷新,它会工作。AJAX代码不会刷新页面。请更具体地说,使用“什么都没有发生”。到底发生了什么,什么没有发生?从跟踪HTTP流量开始,并在浏览器的开发人员工具集中检查JavaScript控制台。