Check my code,plz

来源:6-2 自由编程

晓舟

2022-07-11 20:11:42

package com.imooc.springmvc.controller;
import com.imooc.springmvc.entity.Body;
import com.imooc.springmvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/bmi")
public class URLMappingController {
    @PostMapping("/cacult")
    public String showView(Double height,Double weight, ModelMap modelMap){
       String view = "/um/view.jsp";
       Body body = new Body();
       body.setHeight(height);
       body.setWeight(weight);
       Double bmi = weight /((height/100)*(height/100));
       System.out.println("bmi:"+bmi);
       if(bmi<19) {
           body.setResult("<fieldset>多吃点,太廋了,注意加强营养</fieldset>");
       }else if(bmi > 25){
           body.setResult("<fieldset>该减肥了!注意加强锻炼!</fieldset>");
       }else if(bmi >= 19 && bmi <=25){
           body.setResult("<fieldset>体重正常,注意保持</fieldset>");
       }
       modelMap.addAttribute("b",body);
       return view;
    }
}
package com.imooc.springmvc.entity;
public class Body {
    private Double height;
    private Double weight;
    private String result;
    public Double getHeight() {
        return height;
    }
    public void setHeight(Double height) {
        this.height = height;
    }
    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double weight) {
        this.weight = weight;
    }
    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;
    }
}
<%--
  Created by IntelliJ IDEA.
  User: dxz-DC
  Date: 7/10/2022
  Time: 11:24 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>计算BMI身体指数</title>
</head>
<body>
<form action="/bmi/cacult" method="post">
    <label>计算BMI身体指数</label>
    <br/>
    <label>身高:</label>
    <input name = "height" />
    <label>体重 :</label>
    <input name = "weight" /><br/>
    <input type = "submit" value="提交测试"/>
</form>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: dxz-DC
  Date: 7/10/2022
  Time: 5:43 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h2>${b.result}</h2>
</body>
</html>


写回答

2回答

好帮手慕小蓝

2022-07-12

同学你好,同学的代码符合题目要求,逻辑清晰,书写规范,做的很棒。

祝学习愉快~

0

晓舟

提问者

2022-07-11

package com.imooc.springmvc.controller;
import com.imooc.springmvc.entity.Body;
import com.imooc.springmvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/bmi")
public class URLMappingController {
    @PostMapping("/cacult")
    public String showView(Body body, ModelMap modelMap){
       String view = "/um/view.jsp";
       Double bmi = body.getWeight()/((body.getHeight()/100)*(body.getHeight()/100));
       System.out.println("bmi:"+bmi);
       if(bmi<19) {
           body.setResult("<fieldset>多吃点,太廋了,注意加强营养</fieldset>");
       }else if(bmi > 25){
           body.setResult("<fieldset>该减肥了!注意加强锻炼!</fieldset>");
       }else if(bmi >= 19 && bmi <=25){
           body.setResult("<fieldset>体重正常,注意保持</fieldset>");
       }
       modelMap.addAttribute("b",body);
       return view;
    }
}


0

0 学习 · 16556 问题

查看课程