-2

I have the simple BMIclass and trying to use JSF to receive input and display the result of BMI class. But after click submit button, my xhtml page doesn't return anything. My BMI class:

   public class BMI {
   private static int weight;
   private static int height;
   private static double bmi;
   private  static String bmiCategory;

public static int getWeight() {
    return weight;
}
public void setWeight(int weight) {
    this.weight = weight;
}
public static int getHeight() {
    return height;
}
public void setHeight(int height) {
    this.height = height;
}



public BMI(int weight, int height) {
    super();
    this.weight = weight;
    this.height = height;
    bmi();
    bmiCategory();
}
public BMI() {
    super();
    // TODO Auto-generated constructor stub
}

public  double bmi () {

    double bmi;
    bmi = (double)(703 * weight) / (height * height );  
    return bmi;
}

public  String bmiCategory () {

    if (bmi() < 18.5) {
        bmiCategory = "Under weight";
    } else if (bmi() <= 24.9) {
        bmiCategory = "Normal";
    } else if (bmi() <= 29.9) {
        bmiCategory = "Over weight";
    } else {
        bmiCategory = "Obese";
    }
    return bmiCategory;
}

}

XHTML file:

    <h:body>
<h:form>
<p:panel id="panel" header="BMI Calculator" style="margin-bottom:10px;">
    <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
    <h:panelGrid columns="2" cellpadding="5">
        <h:outputLabel for="weight" value="Weight(in lbs):" />
        <p:inputText id = "weight" value="#{bmiController.bmiBean.weight}" required="true" />

        <h:outputLabel for="height" value="Height(in inches):" />
        <p:inputText id = "height" value="#{bmiController.bmiBean.height}" required="true" />required="true" />


    </h:panelGrid>
</p:panel>

   <p:commandButton value="Calculator" update="panel" actionListener="#{bmiController.calculate}"/>

Controller Class:

  import javax.faces.application.FacesMessage;



 @Named
 @ViewScoped

  public class BmiController implements Serializable {

/**
 * 
 */
  private static final long serialVersionUID = 1L;


  BMI bmiBean = new BMI(); 

public BMI getBmiBean() {
    return bmiBean;
}

public void setBmi (BMI bmiBean) {
    this.bmiBean = bmiBean;
}

public void calculate() {


    String message1 = ("%s Your BMI is" + bmiBean.bmi() + "%s and you are" + bmiBean.bmi());            
    FacesContext context = FacesContext.getCurrentInstance();
    context.addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_INFO,"1234", message1));


}

}

Kranatos
  • 107
  • 2
  • 10
  • why you are using `@SessionScoped` ? It must be `@ViewScoped` in your case – Spartan Oct 14 '15 at 08:48
  • 1
    Which books/tutorials/resources are you using to learn JSF? It's not done the right way. Start here: http://stackoverflow.com/tags/jsf/info – BalusC Oct 14 '15 at 09:39
  • Thanks for your comment. I'm trying to learn JSF by myself and this is the first time I create the managed bean class. I just looked the tutorial here: http://www.mkyong.com/jsf2/jsf-2-textbox-example/ – Kranatos Oct 15 '15 at 02:02

1 Answers1

0

Change your #{BmiController.weight} into #{bmiController.weight} in xhtml page that will work.

Replace all BmiController into bmiController in your XHTML page.

  • Thanks for your help. But I call my managed bean class is BmiController, I think it still doesn't work if I change the name like that – Kranatos Oct 14 '15 at 07:24
  • Normally the managed bean name in the xhtml should start with small letter (for managed bean BmiController it should like bmiController in your xhtml page) other wise you can give your own controller name in @ManagedBean(name="YourCustomName") and use the custom name in your xhtml page. – Natarajan Murugesan Oct 14 '15 at 07:30
  • Remove the bmiBean object creatation and try to create the same bmiBean object in calculate method. – Natarajan Murugesan Oct 14 '15 at 08:53
  • I just modified my xhtml and controller class in #1 post,but I still get the error that "the BMI class don't have readable object weight and height". Can you plz take a look on my code? – Kranatos Oct 15 '15 at 17:36