JSF错误“类'beans.OperationBean'没有属性'operation'。”

时间:2019-09-25 18:20:44

标签: java jsf netbeans javabeans

我遵循了关于getter和setter的所有约定。 numOne和numTwo属性似乎可以正常运行,但操作或numsCrunched属性均无效。我只是想创建一个基本的计算器。如果我在HTML中编写“ OperationBean.operation”,它不会崩溃,但也不会显示任何内容。我觉得要成功执行此操作缺少很多重要信息。

Index.xhtml:

<?xml version='1.0' encoding='UTF8'?>
<!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Number Cruncher</title>
    </h:head>
    <h:body style="background-color:lightgray">
        <f:view>
            <h:outputText style="font-size:large; font-family:serif; font-weight:bold;" value="Number Cruncher"></h:outputText>
            <hr></hr>
            <h:form>
                <h:panelGrid columns="2" >
                <h:outputText value="Number One:"></h:outputText>
                <h:inputText id="numOne" value="#{operationBean.numOne}"></h:inputText>
                <h:outputText value="Number Two:"></h:outputText>
                <h:inputText id="numTwo" value="#{operationBean.numTwo}"></h:inputText>
                </h:panelGrid>
                <hr></hr>
                <h:commandButton value="Add"
                action="#{operationBean.operationAdd}"></h:commandButton>
                <h:commandButton value="Subtract"
                action="#{operationBean.operationSubtract}"></h:commandButton>
                <h:commandButton value="Divide"
                action="#{operationBean.operationDivide}"></h:commandButton>
                <h:commandButton value="Multiply"
                action="#{operationBean.operationMultiply}"></h:commandButton>
                <h:commandButton value="Crunch: #{operationBean.operation}"
                action="#{operationBean.crunch}"></h:commandButton>
            </h:form>
        </f:view>
    </h:body>
</html>

OperationBean.java:

package beans;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
@Named(value = "operationBean")
@SessionScoped
public class OperationBean implements Serializable {
private double numOne;
private double numTwo;
private double numsCrunched;
private String operation;


/**
* Creates a new instance of OperationBean
*/
public OperationBean() {
    //this.operation = "+";
}

public void operationAdd(){
    this.operation = "+";
}
public void operationSubtract(){
    this.operation = "-";
}
public void operationDivide(){
    this.operation = "/";
}
public void operationMultiply(){
    this.operation = "*";
}

/**
* @return the operation
*/
public String getOperation(){
    return operation;
}

/**
* @return the answer
*/
public double getNumsCrunched(){
    return numsCrunched;
}
/**
* @return the numOne
*/
public double getNumOne() {
    return numOne;
}

/**
* @param numOne the first number to set
*/
public void setNumOne(double numOne) {
    this.numOne = numOne;
}

/**
* @return the numTwo
*/
public double getNumTwo() {
    return numTwo;
}

/**
* @param numTwo the second number to set
*/
public void setNumTwo(double numTwo) {
    this.numTwo = numTwo;
}


public void setNumsCrunched(double numsCrunched){
    this.numsCrunched = numsCrunched;
}

public double crunch() {
    /*
    switch (operation) {
        case '+':
            setAnswer(this.numOne + this.numTwo);
            //return this.answer;
        case '-':
             setAnswer(this.numOne - this.numTwo);
            //return this.answer;
        case '/':
             setAnswer(this.numOne / this.numTwo);
            //return this.answer;
        case '*':
             setAnswer(this.numOne * this.numTwo);
            //return this.answer;
        default:
            break;
    }*/
    if("+".equals(this.operation)){
        this.numsCrunched = this.numOne + this.numTwo;
    }else{
        this.numsCrunched = this.numOne - this.numTwo;
    }
    return 0;
 }
}

不幸的是,我不断出现以下错误:

javax.servlet.ServletException: /index.xhtml @30,50 value="Crunch: #{operationBean.operation}": The class 'beans.OperationBean' does not have the property 'operation'.

1 个答案:

答案 0 :(得分:0)

只需清洁,重建和重新部署服务器即可。奇怪的错误。