import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller("DataController")
@RequestMapping("/data")
public class DataController {
@RequestMapping(value={"demo01"}, method={RequestMethod.POST, RequestMethod.GET})
public String demo01(HttpServletRequest request, HttpSession session){
//直接使用API
System.out.println("demo01 is ran…");
request.setAttribute("name", "程州");
session.setAttribute("age", 18);
return "index";
}
//使用Spring MVC框架的Model接口
@RequestMapping("demo02")
public String demo02(Model model){
System.out.println("demo02 is ran…");
model.addAttribute("name", "张三");
return "index";
}
@RequestMapping("demo03")
public String demo03(ModelMap map){
System.out.println("demo03 is ran…");
map.put("name", "李四");
return "index";
}
@RequestMapping("demo04")
public ModelAndView demo04(){
System.out.println("demo04 is ran…");
ModelAndView mav = new ModelAndView();
mav.addObject("name", "老王");
mav.setViewName("index");
return mav;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘index.jsp’ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!–
<link rel="stylesheet" type="text/css" href="styles.css">
–>
</head>
<body>
This is my JSP page. <br>
年龄:${sessionScope.age }<br>
requestName:${requestScope.name }<br>
sessionName:${sessionScope.name }<br>
appName:${applicationScope.name }<br>
</body>
评论