我在 jsp 上创建了一个简单的应用程序..
在我的全局函数 jsp 文件中,我创建了如下函数:
<%! public double calcB(double w, double h){
double B = 0;
return B = (w / (h * h));
}
public String calcClassif(double B){
String classifi = null;
if(B >= 30)
classif = "Obese";
else if(B >= 25)
classif = "Overweight";
else if(B >= 18.5)
classif = "Normal";
else
classif = "Underweight";
return classif;
}
%>
现在在我的 index.jsp 文件中,我编写了以下内容:
<%@include file = "globalFunctions.jsp" %>
<% Boolean submitted = Boolean.parseBoolean(request.getParameter("isSubmitted"));
double we = 0, he = 0;
if(submitted){
weight = Double.parseDouble(request.getParameter("w"));
height = Double.parseDouble(request.getParameter("h"));
}
%>
<h3>BMI Calculator</h3>
<form action = "index.jsp" method = "post">
<input type ="hidden" name = "isSubmitted" value = "true">
Weight: <input type = "text" name = "w"> <br> <br>
Height: <input type = "text" name = "h"> <br> <br>
<input type = "submit" value = "Compute"> <br> <br>
BMI: <%= calcBMI(we, he) %> <br> <br>
Classification: <%= classification %>
</form>
当我执行应用程序时,分类不起作用。如何调用该方法来显示正确的分类? 请帮忙..谢谢
最佳答案
您永远不会将值分配给分类
。你可以试试这个:
<%@include file = "globalFunctions.jsp" %>
<% Boolean submitted = Boolean.parseBoolean(request.getParameter("isSubmitted"));
double we = 0, he = 0;
if(submitted){
weight = Double.parseDouble(request.getParameter("w"));
height = Double.parseDouble(request.getParameter("h"));
bmi = calcBMI(we, he);
classification = calcClassif(bmi);
}
%>
<h3>BMI Calculator</h3>
<form action = "index.jsp" method = "post">
<input type ="hidden" name = "isSubmitted" value = "true">
Weight: <input type = "text" name = "w"> <br> <br>
Height: <input type = "text" name = "h"> <br> <br>
<input type = "submit" value = "Compute"> <br> <br>
BMI: <%= bmi %> <br> <br>
Classification: <%= classification %>
</form>
关于java - 从另一个 jsp 文件调用 JSP 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32227957/