133_Java_SpringMVC_域对象共享数据_视图

网友投稿 649 2022-05-29

域对象共享数据

原始

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

@Controller public class ScopeController { //使用servletAPI向request域对象共享数据 @RequestMapping("/testRequestByServletAPI") public String testRequestByServletAPI(HttpServletRequest request){ request.setAttribute("testRequestScope", "hello,servletAPI"); return "success"; }

MVC -不管哪种方式,最终数据都会被封装到 ModelAndView中

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

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

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

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

6  Model、ModelMap、Map的关系

@RequestMapping("/testModelAndView") public ModelAndView testModelAndView(){ ModelAndView mav = new ModelAndView(); //处理模型数据,即向请求域request共享数据 mav.addObject("testRequestScope", "hello,ModelAndView"); //设置视图名称 mav.setViewName("success"); return mav; } // BindingAwareModelMap // Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的 @RequestMapping("/testModel") public String testModel(Model model){ model.addAttribute("testRequestScope", "hello,model"); System.out.println(model.getClass().getName()); //BindingAwareModelMap return "success"; } @RequestMapping("/testMap") public String testMap(Map map){ map.put("testRequestScope", "hello,map"); System.out.println(map.getClass().getName()); //BindingAwareModelMap return "success"; } @RequestMapping("/testModelMap") public String testModelMap(ModelMap modelMap){ modelMap.addAttribute("testRequestScope", "hello,ModelMap"); System.out.println(modelMap.getClass().getName()); //BindingAwareModelMap return "success"; }

7  向session域共享数据

session 钝化: 服务器关闭,浏览器未关闭 说明会话继续,存储在session的数据会序列化到磁盘上

session 活化: 服务器重新开启 ,将钝化后session的数据重新读取到session中 称为活化

8  向application域共享数据

@RequestMapping("/testSession") public String testSession(HttpSession session){ session.setAttribute("testSessionScope", "hello,session"); return "success"; } @RequestMapping("/testApplication") public String testApplication(HttpSession session){ ServletContext context = session.getServletContext(); context.setAttribute("testApplicationScope", "hello,application"); return "success"; }

首页

首页

通过servletAPI向request域对象共享数据
通过ModelAndView向request域对象共享数据
通过Model向request域对象共享数据
通过map向request域对象共享数据
通过ModelMap向request域对象共享数据
通过servletAPI向session域对象共享数据
通过servletAPI向application域对象共享数据

SpringMVC的视图

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

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

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

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

1 ThymeleafView

当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析,

视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转

2 转发视图SpringMVC中默认的转发视图是InternalResourceView

SpringMVC中创建转发视图的情况:

当控制器方法中所设置的视图名称以"forward:"为前缀时,

创建InternalResourceView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,

而是会将前缀"forward:"去掉,剩余部分作为最终路径通过转发的方式实现跳转

例如"forward:/","forward:/employee"

3 重定向视图SpringMVC中默认的重定向视图是RedirectView

当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,

而是会将前缀"redirect:"去掉,剩余部分作为最终路径通过重定向的方式实现跳转

例如"redirect:/","redirect:/employee"

@Controller public class ViewController { @RequestMapping("/testThymeleafView") public String testThymeleafView(){ return "success"; // 转发浏览器 地址http://localhost:8080/springMVC/testThymeleafView } @RequestMapping("/testForward") public String testForward(){ return "forward:/testThymeleafView"; //http://localhost:8080/springMVC/testForward } @RequestMapping("/testRedirect") public String testRedirect(){ return "redirect:/testThymeleafView"; //http://localhost:8080/springMVC/testThymeleafView } }

Title 测试ThymeleafView
测试InternalResourceView
测试RedirectView

4 视图控制器view-controller

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

133_Java_SpringMVC_域对象共享数据_视图

JSP 页面跳转

MVC配置

<%@ page contentType="text/html;charset=UTF-8" language="java" %> Title

首页

success.jsp

@Controller public class JspController { @RequestMapping("/success") public String success(){ return "success"; } }

Java MVC

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:协同OA系统业务接待管理解决方案
下一篇:汇编语言从键盘输入一个字符串(串长不大于80)以十进制输出字符串中非字母字符的个数(不是a to z或 A to Z)
相关文章