1、ModelAndView
package cn.et.springmvc.lesson03;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* springmvc中model相关对象 是处理和数据相关的对象
* @ModelAttribute 重命名参数数据
* Model(ModelMap|Map) 传递数据到视图(request.setAttribute)
*
*
* ModelAndView 绑定数据到视图(ModelAndView用于传递数据 view对象用于跳转)
*/
@Controller
public class SessionController {
@RequestMapping(value="/case",method=RequestMethod.GET)
public String case1(Map map){ //可以使用Model===model.addAttribute("键",值); ModelMap=====modelMap.put("键",值);
map.put("A","B");
return "/lesson03/res.jsp";
}
@RequestMapping(value="/case2",method=RequestMethod.GET)
public ModelAndView case2(){
ModelAndView mav=new ModelAndView("/lesson03/res.jsp"); //绑定了跳转的视图
//mav.setViewName("/lesson03/res.jsp");
mav.addObject("A","C"); //设值
return mav;
}
}
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'res.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>
${requestScope.A}
</body>
</html>
访问case
访问case2