文章目录
温馨提示:本文章中的动图被压缩后可能不太清晰,可以点击动图鼠标右键在新标签中打开图片就很清晰了。
一、基本环境搭建
项目整体结构
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href = "hello">hello</a><br>
<a href="handle01">handle01</a><br>
<a href="handle02">handle02</a><br>
<a href="handle03">handle03</a><br>
<a href="handle04">handle04</a><br>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
pageContext:${pageScope.msg}<br>
request:${requestScope.msg}<br>
session:${sessionScope.msg} - ${sessionScope.haha}<br>
application:${applicationScope.msg}
</body>
</html>
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 包扫描 -->
<context:component-scan base-package="com.dong"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVC_rest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 字符编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
二、数据输出(Map、Model、MdeolMap)
除了传入原生request和session,SpringMVC还提供了很多其他方式将数据带给页面。
传入Map,Model,ModelMap。给这些参数里面保存的所有数据都会放在域中,可以在页面获取
Map,Model,ModelMap的作用域都是request。
1、Map
index.jsp
controller
@Controller
public class OutputController {
@RequestMapping("/handle01")
public String handle01(Map<String, Object> map) {
map.put("msg", "你好啊");
return "success";
}
}
测试
可见数据放在请求域中
2、Model
index.jsp
controller
@Controller
public class OutputController {
@RequestMapping("/handle02")
public String handle02(Model model) {
model.addAttribute("msg", "我是Model");
return "success";
}
}
测试
可见数据放在请求域中
3、ModelMap
index.jsp
controller
@Controller
public class OutputController {
@RequestMapping("/handle03")
public String handle03(ModelMap modelmap) {
modelmap.addAttribute("msg", "我是ModelMap");
return "success";
}
}
测试
可见数据放在请求域中
三、处理模型数据
1、方法返回值为ModelAndView
index.jsp
controller
@Controller
public class OutputController {
@RequestMapping("/handle04")
public ModelAndView handle04() {
//之前返回值我们就叫视图名:视图名解析器是会帮我们最终拼串得到页面的真实地址
ModelAndView modelAndView = new ModelAndView("success");
modelAndView.addObject("msg","我是ModelAndView");
return modelAndView;
}
}
可见数据放在请求域中
2、使用@SessionAttributes注解给session域中暂存数据
SpringMVC提供了一种可以临时给Session域中保存数据的方式,那就是使用@SessionAttributes注解,但该注解只能标注在类上
使用@SessionAttributes(value = “msg”)注解会在给BindingAwareModelMap中保存数据的同时,为session中存放一份。value指定保存数据时要给session中存放的key。
- value = {“msg”}:只要保存的是这种key的数据,给session中存放一份。
- types={String.class}:只要保存的是这种类型的数据,给session中存放一份。
controller
@SessionAttributes(value = "msg")
@Controller
public class OutputController {
@RequestMapping("/handle01")
public String handle01(Map<String, Object> map) {
map.put("msg", "你好啊");
return "success";
}
@RequestMapping("/handle02")
public String handle02(Model model) {
model.addAttribute("msg", "我是Model");
return "success";
}
@RequestMapping("/handle03")
public String handle03(ModelMap modelmap) {
modelmap.addAttribute("msg", "我是ModelMap");
return "success";
}
@RequestMapping("/handle04")
public ModelAndView handle04() {
//之前返回值我们就叫视图名:视图名解析器是会帮我们最终拼串得到页面的真实地址
ModelAndView modelAndView = new ModelAndView("success");
modelAndView.addObject("msg","我是ModelAndView");
return modelAndView;
}
}
测试
设置保存类型
测试
18并没有出现,可知没有保存到session中,类型判断成功
SpringMVC虽然提供了为session存放数据的@SessionAttributes注解,不过还是推荐使用原始API,因为使用SpringMVC提供的注解可能会引发异常。
觉得博主写的不错的读者大大们,可以点赞关注和收藏哦,谢谢各位!