我已经阅读了 SO 和其他地方关于我的问题的内容。
我明白我所做的事情是完全错误的。我的问题是我不知道如何正确地做这件事,如果有人能帮助我,我会很高兴。
目前我所拥有的看起来像这样:
<ul class="unstyled">
<ui:repeat value="#{paymentDetailsBean.products}" var="product" varStatus="status">
<li>
<div>
<h:selectBooleanCheckbox value="#{product.initiallySelected}">
<f:ajax render="paymentDetailsForm:totalPriceText" listener="#{paymentDetailsBean.updateTotalPrice}"></f:ajax>
</h:selectBooleanCheckbox>
</div>
</li>
</ui:repeat>
</ul>
product.initiallySelected
是一个 boolean 值,我希望在用户选中/取消选中产品时更新该值。
我还使用 ajax 标签来重新渲染:
<h:outputText id="totalPriceText"><b>#{passengerDetailsBean.formatCurrency(paymentDetailsBean.totalPrice.toString())}</b></h:outputText>
基本上我希望更新总价,因为选择/取消选择产品会使其上涨或下跌。我还使用 ajax 标签(显然,这就是问题所在)在后端调用此函数:
public void updateTotalPrice(AjaxBehaviorEvent event)
{
this.totalPrice = 0.0f;
for (Product product : this.getProducts())
{
if (product.getInitiallySelected())
{
this.totalPrice += product.getCurrencyAmountGroup().getAmount();
}
}
this.totalPrice += this.getTotalFaresAmount();
}
所有这些都有效,但速度非常慢,我知道这是因为我不应该使用 ajax 标签来执行业务逻辑。我不知道正确的做法。有人可以帮我吗?
最佳答案
如果您想通过选中/取消选中特定产品来增加/减少总值(value),并假设您的环境支持 EL 2.2+(从您的代码片段判断情况属实),您可以将当前金额存储为 bean 属性,并根据当前选择的产品和复选框的当前值来增加/减少它:
<ui:repeat value="#{paymentDetailsBean.products}" var="product">
<li>
<div>
<h:selectBooleanCheckbox>
<f:ajax render=":paymentDetailsForm:totalPriceText"
listener="#{paymentDetailsBean.updateTotalPrice(product, component.value)}" />
</h:selectBooleanCheckbox>
</div>
</li>
</ui:repeat>
<h:outputText value="#{paymentDetailsBean.totalPrice}" />
与听众:
public void updateTotalPrice(Product product, Boolean direction) {
totalPrice += product.getCurrencyAmountGroup().getAmount() *
(direction ? 1 : -1);
}
关于java - 错误地使用 JSF ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19512701/