从JSP页面检索结果值,并将该结果发布在Spring Boot中的另一个JSP页面。

huangapple 未分类评论49阅读模式
英文:

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)

&lt;body&gt;
 &lt;h1&gt;Please enter your height and weight to know your BMI value and work-out tips&lt;/h1&gt;
	 &lt;c:choose&gt;
      &lt;c:when test=&quot;${mode==&#39;MODE_LOGGED&#39; }&quot;&gt;
	  &lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;metric&quot;&gt;
            &lt;h1&gt;BMI Calculation&lt;/h1&gt;&lt;br&gt;
            &lt;p class = &quot;text&quot; &gt;AGE: &lt;/p&gt;
           &lt;form:label path=&quot;age&quot; class=&quot;age&quot;&gt;Age :&lt;/form:label&gt;
            &lt;form:input type=&quot;number&quot; id=&quot;age&quot; min=&quot;2&quot; max=&quot;100&quot; value=&quot;${user.age }&quot;required /&gt;&lt;br&gt;
            &lt;form:label path=&quot;gender&quot; class = &quot;text&quot; &gt;GENDER: &lt;/form:label&gt;&lt;br&gt;
            &lt;form:label for=&quot;male&quot; class = &quot;text-gender&quot; &gt;MALE&lt;/form:label&gt;
            &lt;form:input type = &quot;radio&quot; id =&quot;male&quot; name=&quot;gender&quot; value=&quot;${user.gender }&quot; required /&gt;
            &lt;form:label for=&quot;female&quot; class = &quot;text-gender&quot; &gt;FEMALE&lt;/form:label&gt;
            &lt;form:input type = &quot;radio&quot; id =&quot;female&quot; name=&quot;gender&quot; value=&quot;${user.gender }&quot; required /&gt;
            &lt;form:label class=&quot;text&quot;&gt;HEIGHT(cm): &lt;/form:label&gt;
            &lt;form:input type = &quot;number&quot; id =&quot;height&quot; value=&quot;${user.height }&quot; required /&gt;
            &lt;form:label class = &quot;text&quot;&gt;WEIGHT(kg): &lt;/form:label&gt;
            &lt;form:input type = &quot;number&quot; id =&quot;weight&quot; value=&quot;${user.weight }&quot;required /&gt;
            &lt;br&gt;
            &lt;form:button id=&quot;button&quot; value=&quot;Calculate&quot;&gt;Result&lt;/form:button&gt;
            
        &lt;/div&gt; 
&lt;/div&gt;
&lt;/c:when&gt;
&lt;/c:choose&gt;
&lt;body&gt;

I want to display my result in the below code of another jsp file (file name:resultpage)

&lt;body&gt;
 &lt;c:choose&gt;
       &lt;c:when test=&quot;${mode==&#39;MODE_RESULT &#39;}&quot;&gt;
      &lt;h1&gt; Your BMI result is ${res}&lt;/h1&gt;
&lt;/body&gt;

This is my controller class

@Controller
public class OnlineController {

	@RequestMapping(value=&quot;/resultpage&quot;, method=RequestMethod.POST)
	public String userResult(ModelMap model,@ModelAttribute BMICalciModel bmicalcimodel, BindingResult result,HttpServletRequest request) {
		onlineService.getBMIResult(bmicalcimodel);
		request.setAttribute(&quot;mode&quot;, &quot;MODE_RESULT&quot;);
		model.addAttribute(&quot;res&quot;,bmicalcimodel.getResult());
		return &quot;resultpage&quot;;
	}

This is my service class

@Service
public class OnlineService {
public String getBMIResult(BMICalciModel bmicalcimodel) {
		String result = null;
		if(bmicalcimodel.getResult()&lt;18.5) {
			result = &quot;Oh My God! You are UNDERWEIGHT! Have some healthy food and gain weight!&quot;;
		}
		else if (bmicalcimodel.getResult()&gt;= 18.5 &amp;&amp; bmicalcimodel.getResult()&lt;25) {
			result = &quot;Congrats! You are perfect with your BMI! Eat healthy and stay fit!&quot;;
		}
		else if (bmicalcimodel.getResult()&gt;= 25 &amp;&amp; bmicalcimodel.getResult()&lt;30){
			result = &quot;The time has come to wear your gym shoes and reduce your fat! Your BMI shows that you are OVERWEIGHT!!&quot;;
			}
		else {
			result = &quot;Please enter correct height and weight!&quot;;
		}
		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(), &quot;%.2f&quot;, 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

huangapple
  • 本文由 发表于 2020年6月29日 11:54:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/62630965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定