英文:
Retrieval of result value from a jsp page and posting that result in another jsp page in spring boot
问题
以下是您要翻译的内容:
这是包含BMI计算器的JSP页面(文件名:BMI.jsp)
<body>
<h1>请填写您的身高和体重,以了解您的BMI值和锻炼建议</h1>
<c:choose>
<c:when test="${mode=='MODE_LOGGED'}">
<div class="container">
<div class="metric">
<h1>BMI计算</h1><br>
<p class="text">年龄:</p>
<form:label path="age" class="age">年龄:</form:label>
<form:input type="number" id="age" min="2" max="100" value="${user.age}" required /><br>
<form:label path="gender" class="text">性别:</form:label><br>
<form:label for="male" class="text-gender">男性</form:label>
<form:input type="radio" id="male" name="gender" value="${user.gender}" required />
<form:label for="female" class="text-gender">女性</form:label>
<form:input type="radio" id="female" name="gender" value="${user.gender}" required />
<form:label class="text">身高(厘米):</form:label>
<form:input type="number" id="height" value="${user.height}" required />
<form:label class="text">体重(公斤):</form:label>
<form:input type="number" id="weight" value="${user.weight}" required />
<br>
<form:button id="button" value="计算">结果</form:button>
</div>
</div>
</c:when>
</c:choose>
<body>
我想在另一个JSP文件的以下代码中显示我的结果(文件名:resultpage)
<body>
<c:choose>
<c:when test="${mode=='MODE_RESULT '}">
<h1>您的BMI结果为${res}</h1>
</body>
这是我的控制器类
@Controller
public class OnlineController {
@RequestMapping(value="/resultpage", method=RequestMethod.POST)
public String userResult(ModelMap model, @ModelAttribute BMICalciModel bmicalcimodel, BindingResult result, HttpServletRequest request) {
onlineService.getBMIResult(bmicalcimodel);
request.setAttribute("mode", "MODE_RESULT");
model.addAttribute("res", bmicalcimodel.getResult());
return "resultpage";
}
这是我的服务类
@Service
public class OnlineService {
public String getBMIResult(BMICalciModel bmicalcimodel) {
String result = null;
if(bmicalcimodel.getResult() < 18.5) {
result = "天哪!您体重过轻!多吃些健康食物,增加体重吧!";
}
else if (bmicalcimodel.getResult() >= 18.5 && bmicalcimodel.getResult() < 25) {
result = "恭喜!您的BMI非常理想!继续健康饮食,保持身材!";
}
else if (bmicalcimodel.getResult() >= 25 && bmicalcimodel.getResult() < 30){
result = "是时候穿上您的运动鞋,减肥啦!您的BMI显示您有些超重!";
}
else {
result = "请输入正确的身高和体重!";
}
return result;
}
}
这是我的模型类
import java.util.Locale;
public class BMICalciModel {
public int age;
public String gender;
public float height;
public float weight;
public float result;
public BMICalciModel() {
super();
}
public BMICalciModel(int age, String gender, float height, float weight, float result) {
super();
this.age = age;
this.gender = gender;
this.height = height;
this.weight = weight;
this.result = result;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getResult() {
return result;
}
public void setResult(float result) {
this.result = result;
}
private float calci() {
result = weight/(height/100*height/100);
return fixedToTwo(result);
}
private float fixedToTwo(float num) {
return Float.valueOf(String.format(Locale.getDefault(), "%.2f", num));
}
}
application.properties已经按照正确的前缀和后缀进行了配置
请给我一个在另一个给定的JSP页面中显示BMI结果值的解决方案。
英文:
I'm designing BMI calculator page using spring boot. I want to show the result of BMI in another jsp file.
I got confused and got struck in the middle. The following codes contain jsp page code, and all classes code.
I'm a beginner in springboot and I may have done some mistakes. So kindly help me out.
This is my JSP page containing BMI Calcultor (file name: BMI.jsp)
<body>
<h1>Please enter your height and weight to know your BMI value and work-out tips</h1>
<c:choose>
<c:when test="${mode=='MODE_LOGGED' }">
<div class="container">
<div class="metric">
<h1>BMI Calculation</h1><br>
<p class = "text" >AGE: </p>
<form:label path="age" class="age">Age :</form:label>
<form:input type="number" id="age" min="2" max="100" value="${user.age }"required /><br>
<form:label path="gender" class = "text" >GENDER: </form:label><br>
<form:label for="male" class = "text-gender" >MALE</form:label>
<form:input type = "radio" id ="male" name="gender" value="${user.gender }" required />
<form:label for="female" class = "text-gender" >FEMALE</form:label>
<form:input type = "radio" id ="female" name="gender" value="${user.gender }" required />
<form:label class="text">HEIGHT(cm): </form:label>
<form:input type = "number" id ="height" value="${user.height }" required />
<form:label class = "text">WEIGHT(kg): </form:label>
<form:input type = "number" id ="weight" value="${user.weight }"required />
<br>
<form:button id="button" value="Calculate">Result</form:button>
</div>
</div>
</c:when>
</c:choose>
<body>
I want to display my result in the below code of another jsp file (file name:resultpage)
<body>
<c:choose>
<c:when test="${mode=='MODE_RESULT '}">
<h1> Your BMI result is ${res}</h1>
</body>
This is my controller class
@Controller
public class OnlineController {
@RequestMapping(value="/resultpage", method=RequestMethod.POST)
public String userResult(ModelMap model,@ModelAttribute BMICalciModel bmicalcimodel, BindingResult result,HttpServletRequest request) {
onlineService.getBMIResult(bmicalcimodel);
request.setAttribute("mode", "MODE_RESULT");
model.addAttribute("res",bmicalcimodel.getResult());
return "resultpage";
}
This is my service class
@Service
public class OnlineService {
public String getBMIResult(BMICalciModel bmicalcimodel) {
String result = null;
if(bmicalcimodel.getResult()<18.5) {
result = "Oh My God! You are UNDERWEIGHT! Have some healthy food and gain weight!";
}
else if (bmicalcimodel.getResult()>= 18.5 && bmicalcimodel.getResult()<25) {
result = "Congrats! You are perfect with your BMI! Eat healthy and stay fit!";
}
else if (bmicalcimodel.getResult()>= 25 && bmicalcimodel.getResult()<30){
result = "The time has come to wear your gym shoes and reduce your fat! Your BMI shows that you are OVERWEIGHT!!";
}
else {
result = "Please enter correct height and weight!";
}
return result;
}
}
And this is my model class
import java.util.Locale;
public class BMICalciModel {
public int age;
public String gender;
public float height;
public float weight;
public float result;
public BMICalciModel() {
super();
}
public BMICalciModel(int age, String gender, float height, float weight, float result) {
super();
this.age = age;
this.gender = gender;
this.height = height;
this.weight = weight;
this.result = result;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getResult() {
return result;
}
public void setResult(float result) {
this.result = result;
}
private float calci() {
result = weight/(height/100*height/100);
return fixedToTwo(result);
}
private float fixedToTwo(float num) {
return Float.valueOf(String.format(Locale.getDefault(), "%.2f", num));
}
}
application.properties are made with correct prefix and suffix
Please give me a solution to display my bmi result value in another given jsp page
专注分享java语言的经验与见解,让所有开发者获益!
评论