1.OutPutController

package com.tz.controller;

import java.util.Map;

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.servlet.ModelAndView;

@Controller
@RequestMapping("/output")
public class OutPutController {

	/**
	 * springmvc除了用servlet的session和request之外还能怎样把数据放哪进行转储
	 *
	 * 1.可以在方法参数的位置上传入Map,model,ModelMap三种类型的任意一种参数
	 * ,往对象中保存数据,都会放到域对象(request)中,可以从页面获取
	 * BindingAwareModelMap
	 *
	 * Map,model,ModelMap,最终都是BindingAwareModelMap再操作,相当于BindingAwareModelMap中,
	 * 保存的数据都会被放在request的域对象中
	 *
	 * Map<jdk> Model<spring> ModelMap<spring>
	 * 								|
	 * 都实现map接口
	 *
	 *
	 * 2.springmvc支持把方法的返回值类型设置ModelAndView类型
	 *
	 * @return
	 */

	@RequestMapping("/handle01")
	public String handle01(Map<String,Object> map){
		map.put("msg","你好");//map会存在request域对象中
		System.out.println("map的类型:"+map.getClass());
		return "success";
	}

	@RequestMapping("/handle02")
	public String handle02(Model model){
		model.addAttribute("msg","你吃饭了吗");
		System.out.println(model.getClass());
		return "success";
	}

	@RequestMapping("/handle03")
	public String handle03(ModelMap modelMap){
		modelMap.addAttribute("msg","ModelMap");
		System.out.println(modelMap.getClass());
		return "success";
	}

	/**
	 * ModelAndView:既包含视图信息(页面地址),也包含模型数据(给页面带的数据)
	 * 而且最终数据也是存在request域对象中
	 * @return
	 */

	@RequestMapping("/handle04")
	public ModelAndView handle04(){
		//构造方法传入jsp名字,springmvc中的视图解析器会帮助我们进行拼接字符串得到页面的真实地址
//		ModelAndView view = new ModelAndView("success");//第一种设置视图的方法
		ModelAndView view = new ModelAndView();//第二种设置视图的方法
		view.setViewName("success");
		//保存数据
		view.addObject("msg","awslDnmd");//保存在request域名里去
		return view;
	}
}

  2.output.jsp

<%@ page language="java" 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>Insert title here</title>
</head>
<body>

	<a href="output/handle01">outPut1</a><br/>
	<a href="output/handle02">outPut2</a><br/>
	<a href="output/handle03">outPut3</a><br/>
	<a href="output/handle04">outPut4ModelAndView</a><br/>

</body>
</html>

  3.success.jsp

<%@ page language="java" 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h1>success</h1>

<%-- 	request:${requestScope.request}<br/>
	session:${sessionScope.session} --%>

pageContext:${pageScope.msg}<br/>
request:${requestScope.msg}<br/>
session:${sessionScope.msg}<br/>
application:${applicationScope.msg}<br/>

</body>
</html>

  总结:

为啥model,modelMap,modelAndView数据都存在request域对象中?

 2.为啥能够访问success.jsp

02-13 17:35