在 JSF 中提交按钮后,xhtml 不返回任何内容
xhtml not returning anything after button submit in JSF
我有简单的 BMIclass 并尝试使用 JSF 接收输入并显示 BMI class 的结果。但是点击提交按钮后,我的 xhtml 页面没有 return 任何内容。
我的体重指数 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 文件:
<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}"/>
控制器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));
}
}
在 xhtml 页面中将您的 #{BmiController.weight} 更改为 #{bmiController.weight} 即可。
将所有 BmiController 替换为 XHTML 页面中的 bmiController。
我有简单的 BMIclass 并尝试使用 JSF 接收输入并显示 BMI class 的结果。但是点击提交按钮后,我的 xhtml 页面没有 return 任何内容。 我的体重指数 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 文件:
<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}"/>
控制器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));
}
}
在 xhtml 页面中将您的 #{BmiController.weight} 更改为 #{bmiController.weight} 即可。
将所有 BmiController 替换为 XHTML 页面中的 bmiController。