Controller包
package Controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class BmiController {
@RequestMapping("/toBmi.do")
public String toBmi() {
return "bmi";
}
@RequestMapping("bmi.do")
public String bmi(HttpServletRequest request) {
String height = request.getParameter("height");
String weight = request.getParameter("weight");
System.out.println("体重:" + weight + ",身高:" + height);
double bmi = Double.parseDouble(weight)/Double.parseDouble(height)/Double.parseDouble(height);
String status="正常";
if(bmi<19) {
status="过轻";
}else if(bmi>24){
status="过重";
}
request.setAttribute("status", status);
return "view";
}
}
---------------------------------------------------------------bmi.jsp-----------------------------------------------------------------------------------------
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>first spring-mvc</title>
</head>
<body style="font-size: 30px;font-style: italic;">
<form action="bmi.do" method="post">
身高:<input name="height"/><br/>
体重:<input name="weight"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
-------------------------------------view.jsp-----------------------------------------------
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body style="font-size: 30px;">
you body is:${status}
</body>
</html>