【问题标题】:Not able to capture data from an InputText inside a datatable in jsf无法从 jsf 中的数据表中的 InputText 捕获数据
【发布时间】:2013-01-02 14:53:01
【问题描述】:

我有一个datatable,其中有两列。

第 1 列:输出文本,由 bean 数据填充。 第2栏:输入文本框

我已将 column1 设为 commandLink,并在其上附加了 actionListener。目的是当我单击 UI 上的一行时,应在 bean 中捕获选定的行数据。

这里是代码:

<h:dataTable id="headerOverrides" styleClass="header" headerClass="headerTable"
  cellpadding="0" cellspacing="0" width="100%">
        <h:column>
            <f:facet name="header">
                <h:commandLink id="payment_Fund_Name" styleClass="header"
                    actionListener="#{pc_Billing.sortColumn}"
                    onmouseover="resetTargetFrame();">
                    <h:outputText style="width:150px" 
                      value="#{bundle.billing_Fund_Name}">                                                    
                    </h:outputText>
                </h:commandLink>
           </f:facet>
        </h:column>
        <h:column>
           <f:facet name="header">
                <h:commandLink id="allocation_Percent" styleClass="header"
                    actionListener="#{pc_Billing.sortColumn}"
                    onmouseover="resetTargetFrame();">
                    <h:outputText style="width:65px" value="#{bundle.billing_Allocation_Percent}">
                    </h:outputText>
                </h:commandLink>                            
           </f:facet>
        </h:column>
</h:dataTable>
</td>
</tr>

<tr>
<td colspan="2">
    <div class="divTable" style="height: 176px" id="overrideTableData">
    <h:dataTable id="overrideTable" value="#{pc_Billing.overrideFundsList}"
                var="currentRow" columnClasses="column" headerClass="header"
                    rowClasses="#{pc_Billing.paymentRowStyles}" binding="#{pc_Billing.allocationOverrideTable}"
                    styleClass="complexTable" cellpadding="2" cellspacing="0"
                    width="100%" border="1" >


          <h:column id="override_Fund">
            <h:commandLink onmouseover="resetTargetFrame();" id="override_FundLink"
                            actionListener="#{pc_Billing.selectFromRow}" immediate="true"
                            styleClass="textTable" style="width:100%">
                     <h:outputText styleClass="textTable"
                               value="#{currentRow.fundName}" style="width:140px">
                      </h:outputText>
            </h:commandLink>
           </h:column>
           <h:column id="override_Allocation">
              <h:inputText id="allocation_Percent" styleClass="textInput" value="#{currentRow.allocation}"
                                disabled="#{!(pc_Billing.loanPayment || pc_Billing.universalLife)}" onblur="totalFrom();"
                                style="width:70px">
                  <f:convertNumber type="percent" />
              </h:inputText>
        </h:column>
    </h:dataTable>

来自 bean 的 selectFromRow 方法

public void selectFromRow(ActionEvent e) {

        if (e.getComponent().getId().equalsIgnoreCase("override_FundLink")) {
            paymentOverrideSelectedRow = (PaymentOverride) allocationOverrideTable.getRowData();
            allocationOverrideTableData.setFundName(paymentOverrideSelectedRow.getFundName());
            allocationOverrideTableData.setAllocation(paymentOverrideSelectedRow.getAllocation());

        }else if(e.getComponent().getId().equalsIgnoreCase("fundLink"))
        {
            preimumOverrideSelectedRow = (PremiumOverride) premiumOverrideTable.getRowData();
            premiumOverrideTableData.setFundName(preimumOverrideSelectedRow.getFundName());
            premiumOverrideTableData.setAmount(preimumOverrideSelectedRow.getAmount());
            premiumOverrideTableData.setEndDate(preimumOverrideSelectedRow.getEndDate());
            premiumOverrideTableData.setInterestRate(preimumOverrideSelectedRow.interestRate);
        }

    FacesContext.getCurrentInstance().renderResponse();
    }

现在发生的事情是我没有从表格上的输入文本框中获取数据。它是空的。我错过了什么? 此外,当selectFromRow 函数执行表格渲染并且我在文本框中输入的数据被清除时。

【问题讨论】:

  • 你为什么不使用action="#{pc_Billing.selectFromRow(myVarOfTable)}"?只需将行对象传递给操作方法public void selectFromRow(someRowObject myObj) { ?
  • @BalusC : 请看一下,现在代码可读了吗?
  • @Daniel:那有什么区别?目前的方法确实有效。问题是immediate="true" 请参考下面的 Szilagyi Donat 的回答
  • 这就是使用 JSF 访问行元素的方式......您将能够删除 binding(JSF 1.X)
  • @Daniel 是的,你所说的看起来很干净。 :-)

标签: java jsf jsf-2


【解决方案1】:

您是否尝试从此处删除 immediate="true"?

<h:commandLink onmouseover="resetTargetFrame();" id="override_FundLink"
               actionListener="#{pc_Billing.selectFromRow}" immediate="true"
               styleClass="textTable" style="width:100%">

我猜这会让你在处理 inputText 的值之前运行你的 selectFromRow 方法。

【讨论】:

  • 你是对的人……那行得通。我可以得到数据。但现在的问题是眼前的不真实。它正在通过所有验证等。有解决方案吗?
  • 也许可以通过以下方式将 f:ajax 标签嵌入到 h:commandLink 中:&lt;h:commandLink ...&gt;&lt;f:ajax execute="..."/&gt;&lt;/h:commandLink&gt; 可以提供帮助。你应该引用 inputText 字段的 id 的 execute 属性。
  • 我正在做的项目最近从 JSF1.2 升级到 JSF2。库已更新,但文件仍具有 .jsp 扩展名。所以我无法使用ajax :(
  • 这有点原始,但在困难的情况下,我通常会从我的实体中删除 bean 验证,并以我可以在需要时调用的方法手动在托管 bean 中进行验证,通常是在保存记录时.
猜你喜欢
  • 2013-02-06
  • 1970-01-01
  • 2011-01-19
  • 2012-09-02
  • 1970-01-01
  • 2013-07-04
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多