0

I have followed all the conventions for getters and setters. The numOne and numTwo properties seem to work, but not the operation nor the numsCrunched property. I'm just trying to create a basic calculator. If I write in the HTML "OperationBean.operation" it does not crash, but it also does not display anything. I feel like there is an important piece of information I am missing to run this succesfully.

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;
 }
}

Unfortunately i keep getting the following error:

javax.servlet.ServletException: /index.xhtml @30,50 value="Crunch: #{operationBean.operation}": The class 'beans.OperationBean' does not have the property 'operation'.
  • Does it happen if you add a `public void setOperation(String)` method? – Selaron Sep 25 '19 at 18:31
  • Same error, even with each operation method calling it with the correct parameter (+,-,/,*) – Carlos Hernandez Sep 25 '19 at 18:38
  • 1
    If the runtime exception doesn't match the code you think you're running, then you're actually not running the code you think you're running. Clean, rebuild, redeploy, restart, etc. – BalusC Sep 25 '19 at 18:44
  • @CarlosHernandez: But the setOperation WAS missing, so you did not follow the bean conventions. And BalusC is right. – Kukeltje Sep 25 '19 at 18:56
  • @Kukeltje I actually had omitted it from this post for simplicity, turns out I just needed to reset it as well. Even in the docs though, it does state that the setter is optional, correct? – Carlos Hernandez Sep 25 '19 at 19:01

1 Answers1

0

Just needed to clean, rebuild, and redeploy server. Weird error.

  • https://stackoverflow.com/questions/42664660/javax-el-propertynotfoundexception-the-class-does-not-have-a-readable-propert/48376203 – Kukeltje Sep 25 '19 at 19:56