目录
2、使用ModelAndView向request域对象共享数据
0、写在前面
域对象:
- Request(请求)、
- Session(浏览器开启关闭)、
- Application(ServletContext:服务器开启关闭)、
- PageContext(JSP)
- getAttribute()
- setAttribute()
- removeAttribute()
钝化:浏览器开着,服务器关闭时,数据会序列化到磁盘即钝化。
活化:浏览器人仍然开着,此时服务器打开时,从磁盘读取数据到session即活化。
web.xml配置内容
- 配置springMVC的编码过滤器
- encoding: 请求编码
- forceResponseEncoding:响应编码
- 配置springMVC的前端控制器
springmvc.xml配置内容
- 扫描器
- 视图解析器
- 注解驱动
1、域对象共享数据
1、使用ServletAPI向request域对象共享数据
@Controller
public class TestController {
@RequestMapping("/index/test")
public String index(HttpServletRequest request){
request.setAttribute("testScope","requestScopeContent");
return "success";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>success</h1>
<p th:text="${testScope}">使用ServletAPI向request域对象共享数据</p>
</body>
</html>
访问:http://localhost:8080/SpringMVC/index/test
2、使用ModelAndView向request域对象共享数据
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
/**
* ModelAndView有Model和View的功能
* Model主要用于向请求域共享数据
* View主要用于设置视图,实现页面跳转
*/
ModelAndView mav = new ModelAndView();
//向请求域共享数据
mav.addObject("testScope", "hello,ModelAndView");
//设置视图,实现页面跳转
mav.setViewName("success");
return mav;
}
注意:ModelAndView必须作为方法返回值。
3、使用Model向request域对象共享数据
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
4、使用map向request域对象共享数据
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}
5、使用ModelMap向request域对象共享数据
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}
6、Model、ModelMap、Map的关系
Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的
public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
7、向session域共享数据
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,session");
return "success";
}
8、向application域共享数据
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
return "success";
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>success</h1>
<p th:text="${testScope}"></p>
<p th:text="${session.testSessionScope}"></p>
<p th:text="${application.testApplicationScope}"></p>
</body>
</html>
2、SpringMVC的视图
Jsp是动态代码:因为可以写java代码<% System.out.println("aaa") %>
SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户
SpringMVC视图的种类很多,默认有转发视图和重定向视图
- 当工程引入jstl的依赖,转发视图会自动转换为JstlView。
- 若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的是ThymeleafView。
三种返回视图方式:
- return "hello";
- return "forward:/testHello";
- return "redirect:/testHello";
1、ThymeleafView
- 当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析,视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转。
@RequestMapping("/testHello")
public String testHello(){
return "hello";
}
2、转发视图
SpringMVC中默认的转发视图是InternalResourceView
- SpringMVC中创建转发视图的情况:当控制器方法中所设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"forward:"去掉,剩余部分作为最终路径通过转发的方式实现跳转。
- 转发时只能通过控制器在访问html等页面,不能直接转发到某个html页面。
- 转发时可以获得域对象数据。
- 转发时可以访问web-inf下的内容,进行了安全设置,只能通过服务器内部访问,浏览器不可访问。
例如"forward:/"(转发到首页),“forward:/employee”
@RequestMapping("/testForward")
public String testForward(){
return "forward:/testHello";
}
3、重定向视图
SpringMVC中默认的重定向视图是RedirectView。
- 当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:"去掉,剩余部分作为最终路径通过重定向的方式实现跳转。
- 重定向是地址发生了变化,例如增删改查后地址会发生变化,即使用重定向。
- 重定向时不可以获得域对象数据,地址发生了变化。
例如"redirect:/"(重定向到首页),“redirect:/employee”
@RequestMapping("/testRedirect")
public String testRedirect(){
return "redirect:/testHello";
}
注:
重定向视图在解析时,会先将redirect:前缀去掉,然后会判断剩余部分是否以/开头,若是则会自动拼接上下文路径
4、视图控制器view-controller
- 当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用view-controller标签进行表示。
- 即当前请求映射对应的控制器方法中,只进行页面跳转,没有其他逻辑时,使用该标签
注:
当SpringMVC的springmvc.xml中设置任何一个view-controller标签时,其他控制器中的请求映射将全部失效,此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签:
<mvc:annotation-driven />
所以:
设置了
<mvc:view-controller path="/" view-name="index"/>
则必须设置:
<!-- 开启mvc注解驱动 -->
<mvc:annotation-driven/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.crane.mvc.controller"></context:component-scan>
<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<!-- 视图解析器优先级 -->
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
<mvc:view-controller path="/" view-name="index"/>
<!-- 开启mvc注解驱动 -->
<mvc:annotation-driven/>
</beans>