计算值插入值

时间:2015-06-24 09:53:52

标签: jsf jsf-2.2

我想创建一个非常简单的JSF网页,我想用它来计算价格:

<h:form>
    <h:outputText value="Number of Computers"/>

    <h:panelGroup layout="block" id="desktopClients_select" style="padding-top: 3px;" styleClass="text">
        <h:inputText id="desktopClients" value="#{pricingCalculator.computers}">
            <f:ajax event="change" render="@form" listener="#{pricingCalculator.calculateTotalPrice}"/>
        </h:inputText>
    </h:panelGroup>

    <h:outputText value="Email support incidents"/>

    <h:panelGroup layout="block" id="email_support_incidents" style="padding-top: 3px;" styleClass="text">
        <h:inputText id="email_support_incidents_text" value="#{pricingCalculator.emailSupportIncidents}">
            <f:ajax event="change" render="@form" listener="#{pricingCalculator.calculateTotalPrice}"/>
        </h:inputText>
    </h:panelGroup>

    <h:outputText value="Phone support incidents"/>

    <h:panelGroup layout="block" id="phone_support_incidents" style="padding-top: 3px;" styleClass="text">
        <h:inputText id="phone_support_incidents_text" value="#{pricingCalculator.phoneSupportIncidents}">
            <f:validateLongRange minimum="1" maximum="150" />
            <f:ajax event="change" render="@form" listener="#{pricingCalculator.calculateTotalPrice}"/>
        </h:inputText>
    </h:panelGroup>

    <h:outputText value="Total price"/>

    <h:panelGroup layout="block" id="total_price" style="padding-top: 3px;" styleClass="text">
        <h:outputText value="#{pricingCalculator.calculateTotalPrice}"/>
    </h:panelGroup>

    </h:panelGrid>

</h:form>

豆:

@Named
@ViewScoped
public class PricingCalculator implements Serializable
{
    private int computers;
    private float emailSupportIncidents;
    private float phoneSupportIncidents;

    private float totalPrice;

    // Prices for each component and service    
    private final float computers_price = 299;
    private final float emailSupportIncidents_price = 300;
    private final float phoneSupportIncidents_price = 150;

    public String getCalculateTotalPrice()
    {
        totalPrice = (computers_price * computers)
            + (emailSupportIncidents_price * emailSupportIncidents)
            + (phoneSupportIncidents_price * phoneSupportIncidents);

        String result = Float.toString(totalPrice);
        return result;
    }

    public int getComputers()
    {
        return computers;
    }

    public void setComputers(int computers)
    {
        this.computers = computers;
    }

    public float getEmailSupportIncidents()
    {
        return emailSupportIncidents;
    }

    public void setEmailSupportIncidents(float emailSupportIncidents)
    {
        this.emailSupportIncidents = emailSupportIncidents;
    }

    public float getPhoneSupportIncidents()
    {
        return phoneSupportIncidents;
    }

    public void setPhoneSupportIncidents(float phoneSupportIncidents)
    {
        this.phoneSupportIncidents = phoneSupportIncidents;
    }

    public float getTotalPrice()
    {
        return totalPrice;
    }

    public void setTotalPrice(float totalPrice)
    {
        this.totalPrice = totalPrice;
    }
}

当我在输入字段中插入一些值时,我想重新计算总价。我现在有两个问题:我得到例外serverError: class javax.el.MethodNotFoundException /pricing_calculator.xhtml @115,136 listener="#{pricingCalculator.calculateTotalPrice}": Method not found: com.web.common.PricingCalculator@22939533.calculateTotalPrice(javax.faces.event.AjaxBehaviorEvent)

当我插入新值时,前一个值设置为零。它没有被正确记住。

1 个答案:

答案 0 :(得分:2)

@BalusC已经给你答案,但为了更明确,这里引用了Oracle的Java EE 6教程(你应该阅读)关于方法表达式

  

方法表达式只能在标签属性中使用,并且只能在   以下方式:

     
      
  • 使用单个表达式构造,其中bean引用JavaBeans组件,方法引用JavaBeans的方法   成分:

         

    <some:tag value="#{bean.method}"/>

  •   
     

[...]

也就是说,将getCalculateTotalPrice重命名为calculateTotalPrice

编辑:以下方法调用的预期签名之间也存在不一致:

  • <f:ajax listener="#{pricingCalculator.calculateTotalPrice} .../> 需要MethodExpression引用calculateTotalPrice(javax.faces.event.AjaxBehaviorEvent)方法明确说明javax.el.MethodNotFoundException

  • <h:outputText value="#{pricingCalculator.calculateTotalPrice}"/>期望java.lang.String可以引用bean属性,或者是方法调用的返回值。

此处,<h:outputText>值可能意味着设置为totalPrice属性,因此它应为:<h:outputText value="#{pricingCalculator.totalPrice}"/>