Retrieval of result value from a jsp page and posting that result in another jsp page in spring boot

Viewed 182

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

0 Answers
Related