提问
@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回答
2023版Java工程师
- 参与学习 人
- 提交作业 8788 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星