栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC(05) -- 域对象共享数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC(05) -- 域对象共享数据

SpringMVC学习笔记

源码地址

5.1)向request域对象共享数据

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

使用ServletAPI向request域对象共享数据,代码如下:

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

首页 index.html,代码如下:




    
    Title


首页
通过servletAPI向request域对象共享数据

跳转页 success.html,代码如下:




    
    Title


success

【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/】,点击超链接“通过servletAPI向request域对象共享数据”,页面跳转至:http://localhost:8080/SpringMvcDemo3/testRequestByServletAPI,页面如下:

 

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

使用ModelAndView向request域对象共享数据,代码如下:

    // 使用ModelAndView向request域对象共享数据
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView() {
        ModelAndView mav = new ModelAndView();
        //处理模型数据,即向请求域request共享数据
        mav.addObject("testRequestScope", "hello,ModelAndView");
        //设置视图名称
        mav.setViewName("success");
        return mav;
    }

修改首页 index.html,代码如下:

通过ModelAndView向request域对象共享数据

【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/】,点击超链接2页面跳转至:http://localhost:8080/SpringMvcDemo3/testModelAndView,页面如下:

 

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

使用Model向request域对象共享数据,代码如下:

    // 3)使用Model向request域对象共享数据
    @RequestMapping("/testModel")
    public String testModel(Model model) {
        model.addAttribute("testRequestScope", "hello,model");
        System.out.println("testModel当前使用类 =" + model.getClass().getName());
        return "success";
    }

修改首页 index.html,代码如下:

3)通过Model向request域对象共享数据

【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/】,点击超链接3页面跳转至:http://localhost:8080/SpringMvcDemo3/testModel,页面如下:

 

控制台输出:

18:23:05.692 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/SpringMvcDemo3/testModel", parameters={}
18:23:05.692 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.study.mvc.controller.ScopeController#testModel(Model)
testModel当前使用类 =org.springframework.validation.support.BindingAwareModelMap
18:23:05.694 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK
​

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

使用map向request域对象共享数据,代码如下:

    // 4)使用map向request域对象共享数据
    @RequestMapping("/testMap")
    public String testMap(Map map) {
        map.put("testRequestScope", "hello,map");
        System.out.println("testMap当前使用类 =" + map.getClass().getName());
        return "success";
    }

修改首页 index.html,代码如下:

4)通过map向request域对象共享数据

【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/】,点击超链接4页面跳转至:http://localhost:8080/SpringMvcDemo3/testMap,页面如下:

 

控制台输出:

18:19:41.133 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/SpringMvcDemo3/testMap", parameters={}
18:19:41.133 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.study.mvc.controller.ScopeController#testMap(Map)
testMap当前使用类 =org.springframework.validation.support.BindingAwareModelMap
18:19:41.310 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK

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

使用ModelMap向request域对象共享数据,代码如下:

    // 5)使用ModelMap向request域对象共享数据
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap) {
        modelMap.addAttribute("testRequestScope", "hello,ModelMap");
        System.out.println("testModelMap当前使用类 =" + modelMap.getClass().getName());
        return "success";
    }

修改首页 index.html,代码如下:

5)通过ModelMap向request域对象共享数据

【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/】,点击超链接5页面跳转至:http://localhost:8080/SpringMvcDemo3/testModelMap,页面如下:

 控制台输出:

18:29:20.910 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/SpringMvcDemo3/testModelMap", parameters={}
18:29:20.911 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.study.mvc.controller.ScopeController#testModelMap(ModelMap)
testModelMap当前使用类 =org.springframework.validation.support.BindingAwareModelMap
18:29:21.118 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK

5.1.6)Model、ModelMap、Map的关系

Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的,上述控制台的日志如下:

testModel当前使用类 =org.springframework.validation.support.BindingAwareModelMap
testMap当前使用类 =org.springframework.validation.support.BindingAwareModelMap
testModelMap当前使用类 =org.springframework.validation.support.BindingAwareModelMap

通过查看代码可知三者关系如下:

public interface Model{}
public class ModelMap extends LinkedHashMap {}
public class ExtendedModelMap extends ModelMap implements Model {}
​
public class BindingAwareModelMap extends ExtendedModelMap {}
5.2)向session域共享数据

使用servletAPI向session域对象共享数据,代码如下:

    // 6)向session域共享数据
    @RequestMapping("/testSession")
    public String testSession(HttpSession session) {
        // 通常在用户登录后获取用户信息中使用,当30分钟无相关操作则自动失效
        session.setAttribute("testSessionScope", "hello,session");
        return "success";
    }

修改首页 index.html,代码如下:

6)通过servletAPI向session域对象共享数据

修改跳转页 success.html,代码如下:




    
    Title


success

【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/】,点击超链接6页面跳转至:http://localhost:8080/SpringMvcDemo3/testModelMap,页面如下:

 

5.3)向application域共享数据

通过servletAPI向application域对象共享数据,代码如下:

    // 7)向application域共享数据
    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session) {
        // 范围为整个工程,只有全工程共享才会用到application域共享
        ServletContext application = session.getServletContext();
        application.setAttribute("testApplicationScope", "hello,application");
        return "success";
    }

修改首页 index.html,代码如下:

7)通过servletAPI向application域对象共享数据

修改跳转页 success.html,代码如下:




    
    Title


success

【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/】,点击超链接7页面跳转至:http://localhost:8080/SpringMvcDemo3/testApplication,页面如下:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/924667.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号