提问
@PostMapping("/calculation") @ResponseBody public String showResult(Double height, Double weight){ // ModelAndView mav = new ModelAndView("/result.jsp"); Double out = weight / Math.pow((height / 100),2); String s; if(out <= 19){ s = "多吃点,太瘦了"; }else if(out > 25){ s = "少吃点,太胖了"; }else{ s = "体重正常"; } // mav.addObject("i",s); // return mav; String res = "<h1>提示信息:" + s + "<h1>"; return res; }
写responseBody直接返回字符串,可以实现
但是要用ModelandView的话
@PostMapping("/calculation") // @ResponseBody public ModelAndView showResult(Double height, Double weight){ ModelAndView mav = new ModelAndView("/result.jsp"); Double out = weight / Math.pow((height / 100),2); String s; if(out <= 19){ s = "多吃点,太瘦了"; }else if(out > 25){ s = "少吃点,太胖了"; }else{ s = "体重正常"; } mav.addObject(s); return mav; // String res = "<h1>提示信息:" + s + "<h1>"; // return res; }
然后html
<%-- Created by IntelliJ IDEA. User: Zakaji Li Date: 2023/3/9 Time: 18:51 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> <h1>提示信息:${s}</h1> </body> </html>
却不显示s的内容,这是为什么呢
9
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星