【问题标题】:xhtml not returning anything after button submit in JSF在JSF中提交按钮后xhtml不返回任何内容
【发布时间】:2016-01-12 03:11:54
【问题描述】:

我有简单的 BMI 类,并尝试使用 JSF 接收输入并显示 BMI 类的结果。但是单击提交按钮后,我的 xhtml 页面没有返回任何内容。 我的 BMI 课程:

   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 文件:

    <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}"/>

控制器类:

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


}

}

【问题讨论】:

  • 你为什么使用@SessionScoped?在你的情况下它必须是@ViewScoped
  • 您使用哪些书籍/教程/资源来学习 JSF?它没有以正确的方式完成。从这里开始:stackoverflow.com/tags/jsf/info
  • 感谢您的评论。我正在尝试自己学习 JSF,这是我第一次创建托管 bean 类。我刚刚在这里查看了教程:mkyong.com/jsf2/jsf-2-textbox-example

标签: jsf managed-bean


【解决方案1】:

在 xhtml 页面中将你的 #{BmiController.weight} 更改为 #{bmiController.weight} 即可。

在您的 XHTML 页面中将所有 BmiController 替换为 bmiController。

【讨论】:

  • 感谢您的帮助。但是我称我的托管bean类是BmiController,我认为如果我这样改名它仍然不起作用
  • 通常情况下,xhtml 中的托管 bean 名称应以小写字母开头(对于托管 bean BmiController,它应该类似于 xhtml 页面中的 bmiController),否则您可以在 @ManagedBean(name= "YourCustomName") 并在您的 xhtml 页面中使用自定义名称。
  • 删除 bmiBean 对象创建,并尝试在计算方法中创建相同的 bmiBean 对象。
  • 我刚刚在 #1 帖子中修改了我的 xhtml 和控制器类,但我仍然收到“BMI 类没有可读的对象重量和高度”的错误。你能看看我的代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-05
  • 2019-06-14
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
相关资源
最近更新 更多