麻烦看下我的代码是否正确?我运行会产生404错误
bmi1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>结果显示</title>
</head>
<body>
<h1>${b.getResult()}</h1>
</body>
</html>
cacult.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>计算BMI身体指数</title>
</head>
<body>
<fieldset>
<legend>
<form action="/bmi1" method="post">
身高:<input name="height">cm
体重:<input name="weight">kg
<input type="submit" value="提交测试">
</form>
</legend>
</fieldset>
</body>
</html>
BmiController
@Controller
public class BmiController {
@PostMapping("/bmi1")
public ModelAndView showbmi(Float height,Float weight){
ModelAndView mav=new ModelAndView("/bmi1.jsp");
Bmi bmi=new Bmi();
Float bmiNum=weight/((height/100)*(height/100));
String info;
if(bmiNum<19){
bmi.setResult("多吃点,太瘦了!注意加强营养~");
}else if(bmiNum>25){
bmi.setResult("该减肥了!注意加强锻炼~");
}else {
bmi.setResult("体重正常,注意保持~");
}
mav.addObject("b",bmi);
return mav;
}
}
Bmi实体类
public class Bmi {
private Float height;
private Float weight;
private String result;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
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;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.imooc.springmvc"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星