简体   繁体   English

错误地使用JSF ajax调用

[英]Using JSF ajax call incorrectly

I've read around SO and elsewhere about my problem. 我在SO和其他地方读过有关我的问题的文章。

I understand that what I'm doing is completely wrong. 我了解我在做什么完全是错误的。 My problem is that I don't know how to do it right , and I would love it if someone could help me out. 我的问题是,我不知道该怎么做是正确的,我会喜欢它,如果有人可以帮助我。

Currently what I have looks like this: 目前,我看起来像这样:

<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 is a boolean value that I want updated when the user checks/unchecks the product. product.initiallySelected是当用户检查/取消选中产品时要更新的布尔值。

I'm also using an ajax tag to rerender this: 我也使用ajax标记重新呈现此内容:

<h:outputText id="totalPriceText"><b>#{passengerDetailsBean.formatCurrency(paymentDetailsBean.totalPrice.toString())}</b></h:outputText>

Basically I want the total price ot be updated, because selecting/unselecting a product will make it go up or down. 基本上,我希望总价格可以更新,因为选择/取消选择产品会使价格上升或下降。 I'm also using the ajax tag(And this is where the problem lies, obviously) to call this function in the backend: 我也在使用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();
}

All of this works, but it's extremely slow, and I know it's because I shouldn't be doing business logic using the ajax tag. 所有这些都有效,但是速度非常慢,我知道这是因为我不应该使用ajax标记进行业务逻辑。 I don't know the correct way of doing it. 我不知道这样做的正确方法。 Could anyone please help me? 谁能帮我吗?

In case you want to increase/decresase total value by checking/unchecking a particular product, and assuming your environment supports EL 2.2+ (what is true judging from your snippet), you can store the current amount as a bean property and increase/decrease it depending on the currently selected product and current value of a checkbox: 如果您想通过选中/取消选中某个产品来增加/减少总价值,并且假设您的环境支持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}" />

With the listener: 与听众:

public void updateTotalPrice(Product product, Boolean direction) {
    totalPrice += product.getCurrencyAmountGroup().getAmount() *
                       (direction ? 1 : -1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM