域对象共享数据/SpringMVC的视图

128 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第8天,点击查看活动详情

五、域对象共享数据

1、使用ServletAPI向request域对象共享数据

跟javaEE中的API一样。使用request.getParameter()这个方法获取参数。

2、使用ModelAndView向request域对象共享数据

 /**
  * @author rop
  * @since 2022/7/31 14:58
  */
 @Controller
 public class TestDoMainController {
     @RequestMapping("/test/mav")
     public ModelAndView testModelAndView(){
         /*
          * ModelAndView包含Model和View的功能
          * Model:向请求域中共享数据
          * View:设置逻辑视图实现页面跳转
          */
         ModelAndView mav = new ModelAndView();
         // 向请求域中共享数据
         mav.addObject("testDoMain","Hello ModelAndView");
         // 设置逻辑视图
         mav.setViewName("success");
         return mav;
     }
 }
 <!DOCTYPE html>
 <html lang="en" xmlns:th=http://www.thymeleaf.org>
 <head>
     <meta charset="UTF-8">
     <title>Title</title>
 </head>
 <body>
 <h1>success</h1>
 <p th:text="${testDoMain}"></p>
 </body>
 </html>

ModelAndView的底层是个map,使用的是key/value键值对的方式。

3、使用Model向request域对象共享数据

 @RequestMapping("/test/model")
 public String testModel(Model model){
     model.addAttribute("testDoMain","Hello Model");
     return "success";
 }

4、使用map向request域对象共享数据

 @RequestMapping("/test/map")
 public String testMap(Map<String,Object> map){
     map.put("testDoMain","Hello Map");
     return "success";
 }

5、使用ModelMap向request域对象共享数据

 @RequestMapping("/test/modelMap")
 public String testModelMap(ModelMap modelMap){
     modelMap.addAttribute("testDoMain","Hello ModelMap");
     return "success";
 }

6、Model、ModelMap、Map的关系

在底层,这些类型的形参最终都是通过BindingAwareModelMap创建

7、向session域共享数据

 @RequestMapping("/test/session")
 public String testSession(HttpSession session){
     session.setAttribute("testSession","Hello Session");
     return "success";
 }
 <a th:href="@{/test/session}">向会话域共享数据</a><br>
 <a th:href="@{/test/application}">向应用域共享数据</a><br>
 <!DOCTYPE html>
 <html lang="en" xmlns:th=http://www.thymeleaf.org>
 <head>
     <meta charset="UTF-8">
     <title>Title</title>
 </head>
 <body>
 <h1>success</h1>
 <p th:text="${testDoMain}"></p>
 <p th:text="${session.testSession}"></p>
 <p th:text="${application.testApplication}"></p>
 </body>
 </html>

8、向application域共享数据

 @RequestMapping("/test/application")
 public String testApplication(HttpSession session){
     ServletContext servletContext = session.getServletContext();
     servletContext.setAttribute("testApplication","Hello Application");
     return "success";
 }

六、SpringMVC的视图

SpringMVC中的视图是View接口,视图的作用是渲染数据,将模型Model中的数据展示给用户

SpringMVC视图的种类很多,默认有转发视图和重定向视图

当工程引入jstl的依赖,转发视图会自动转换为jstlView

若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的的是ThymeleafView

1、ThymeleafView

当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析,视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转。

 @RequestMapping("/testHello")
 public String testHello(){
     return "success";
 }

2、转发视图(不怎么用,知道就行)

 <a th:href="@{/test/internal}">InternalResourceView转发视图</a><br>
 @RequestMapping("/test/internal")
 public String testInternal(){
     return "forward:/test/model";
 }

3、重定向视图

 <a th:href="@{/test/redirect}">RedirectView重定向视图</a><br>
 @RequestMapping("/test/redirect")
 public String testRedirect(){
     return "redirect:/test/model";
 }

重定向的时候,重定向的路径也会在地址上显示,转发不会。

4、视图控制器view-controller

当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用view-controller标签进行使用。

 <!--开启mvc注解驱动-->
 <mvc:annotation-driven/>
 ​
 <!--视图控制器:为当前的请求直接设置视图名称实现页面跳转-->
 <mvc:view-controller path="/" view-name="index"/>

有了这两行,就不需要之前的那个以/为路径的Controller方法