SpringMVC之请求响应(上)
2024-10-08 03:15:19
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
最新文章
- Windows下使用Xshell建立反向隧道
- Golang gzip的压缩和解压
- MsXml创建和解析XML示例
- Android中如何收听特定应用安装成功的广播
- Validate Binary Search Tree
- Python之几种重要的基本类型:元组,列表,字典,字符串,集合
- JavaWeb---总结(十)HttpServletRequest对象(一)
- MySQL与SqlServer中update操作同一个表问题
- Windows 7 下如何设置机器级别的DCOM权限
- Java-J2SE学习笔记-查找一个String中,subString的出现次数
- MAC 13信道
- Loadrunner模拟Json请求
- PhotoShop—剪贴蒙版
- 【转】关于C#使用Excel的数据透视表的例子
- 2DPlatformer-SLua 编辑器 UI 美化
- ASP.Net Core简介
- monkey源码分析之事件注入方法变化
- jQuery基础教程
- python爬虫入门---第四篇:网站对爬虫的限制及突破测试
- 洛谷 P1032 字串变换
热门文章
- Go-map-字符串-指针-结构体
- ZJNU 1269 - 灯塔——高级
- Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之列表类型(list)
- CodeForces 990B Micro-World(思维、STL)
- column命令
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】
- Spire.doc jar包实现word文件添加水印demo
- 详解python可迭代对象、迭代器和生成器
- openfire配置好文
- java连接access的用户名、密码异常Decoding not supported解决