我想创建非常简单的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)
当我插入新值时,前一个设置为零。记不清了。
@BalusC已经给出了答案,但更明确地说,这里有一个来自Oracle的Java EE 6教程(你应该阅读(关于方法表达式的引用:
方法表达式只能在标记属性中使用,并且只能在 以下方式:
使用单个表达式构造,其中 bean 引用 JavaBeans 组件,方法引用 JavaBeans 的方法 元件:
<some:tag value="#{bean.method}"/>
[...]
也就是说,将getCalculateTotalPrice
重命名为calculateTotalPrice
编辑:以下方法调用的预期签名之间也存在不一致:
-
<f:ajax listener="#{pricingCalculator.calculateTotalPrice} .../>
期望MethodExpression
参考javax.el.MethodNotFoundException
明确规定的calculateTotalPrice(javax.faces.event.AjaxBehaviorEvent)
方法
和
-
<h:outputText value="#{pricingCalculator.calculateTotalPrice}"/>
,它需要一个可以引用 Bean 属性或方法调用的返回值的java.lang.String
。
在这里,<h:outputText>
值可能意味着设置为 totalPrice
属性,因此它应该是:<h:outputText value="#{pricingCalculator.totalPrice}"/>