SpringMVC中域对象共享数据

域对象共享数据

由于现在使用是Thymeleaf视图解析器,所以以下只对三种域对象进行解析
1.rueqest
2.session
3.application[本质就是servletContext域对象]

thymeleaf常用语法

Thymeleaf语法:

`@{}`:表示跳转路径
`${}`:表示获取共享域中的数据
	`request`存储共享数据,如:`request.setAttribute("scope","hello RequestAPI");`
	 直接:`${scope}`//直接写共享域中的键名,返回:hello RequestAPI
	 
	 `session`存储共享数据,如:`session.setAttribute("scope","hello Session");`
	 `${session.scope}`//返回Session共享域中的数据,返回:hello Session
	 
	 `ServletContext`存储共享数据,如:`servletContext.setAttribute("scope","hello servletContext");
	 `${appliaction.scope}`//返回servletContext共享域中的数据,返回:hello servletContext

一:Request域对象

方式一:使用ServletAPI原始API的方式存储共享数据

使用原始API的方式向共享域中存储数据,HttpSetvletRequst请求对象中存储数据

案例

@Controller
@RequestMapping("/share")
public class ShareController {
    // 原始API方式
    @RequestMapping("/servletAPI")
    public String servletAPI(HttpServletRequest request){
        request.setAttribute("servletAPI","使用ServletAPI方式共享数据");
        return "/share/share";
    }
}

跳转到指定页面,并且在页面中获取request域中存储数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>共享数据</title>
    <style>
        * {
          margin: 0px auto;
        }
        .div {
            width: 500px;
        }
    </style>
</head>
<body>
<div class="div">
    <h3 style="color: green" th:text="${servletAPI}"></h3>
</div>
</body>
</html>

页面显示
在这里插入图片描述

方式二:使用ModelAndViewrequest域对象共享数据[非常重要]

注意使用ModelAndView必须返回值类型ModelAndView类型,前端控制器才能解析,否则前端控制器无法处理[就是相当前端控制器不知该ModelAndView模型]ModelAndViewModelView的功能,Model主要用于向请求域共享数据,View主要用于设置视图,实现页面跳转

案例

    // 使用SpringMVC提供的方式:ModelAndView
    @RequestMapping("/modelAndView")
    public ModelAndView modelAndView(){
        // 步骤1:创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        // Model中储存数据
        mv.addObject("modelAndView","使用ModelAndView方式共享数据");
        // 设置跳转视图名称
        mv.setViewName("/share/share");
        return mv;
    }

跳转到指定页面,并且在页面中获取request域中存储数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>共享数据</title>
    <style>
        * {
          margin: 0px auto;
        }
        .div {
            width: 500px;
        }
    </style>
</head>
<body>
<div class="div">
    <h1 style="color: red">SpringMVC常用三个共享域对象</h1>
    <h3 style="color: green" th:text="${servletAPI}"></h3>
    <h3 style="color: green" th:text="${modelAndView}"></h3>
</div>
</body>
</html>

页面显示
在这里插入图片描述

方式三:使用maprequest域对象共享数据

使用Map集合在形参位置传入一个Map<String,Object>集合,会自动将数据封装放到request共享中

案例

    // 使用Map集合方式
    @RequestMapping("/map")
    public String map(Map<String,Object> map){
        map.put("map","使用Map集合方式共享数据");
        // 设置跳转视图名称
        return "/share/share";
    }

跳转到指定页面,并且在页面中获取request域中存储数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>共享数据</title>
    <style>
        * {
          margin: 0px auto;
        }
        .div {
            width: 500px;
        }
    </style>
</head>
<body>
<div class="div">
    <h1 style="color: red">SpringMVC常用三个共享域对象</h1>
    <h3 style="color: green" th:text="${servletAPI}"></h3>
    <h3 style="color: green" th:text="${modelAndView}"></h3>
    <h3 style="color: green" th:text="${map}"></h3>
</div>
</body>
</html>

页面显示
在这里插入图片描述

方式四:使用Modelrequest域对象共享数据

使用Model对象向request对象中共享数据,其实就是ModelAndView中的Model对象一样,用于向request域中存储共享数据的

案例

    // 使用Model方式
    @RequestMapping("/model")
    public String model(Model model){
        model.addAttribute("model","使用Model方式共享数据");
        return "/share/share";
    }

跳转到指定页面,并且在页面中获取request域中存储数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>共享数据</title>
    <style>
        * {
          margin: 0px auto;
        }
        .div {
            width: 500px;
        }
    </style>
</head>
<body>
<div class="div">
    <h1 style="color: red">SpringMVC常用三个共享域对象</h1>
    <h3 style="color: green" th:text="${servletAPI}"></h3>
    <h3 style="color: green" th:text="${modelAndView}"></h3>
    <h3 style="color: green" th:text="${map}"></h3>
    <h3 style="color: green" th:text="${model}"></h3>
</div>
</body>
</html>

页面显示
在这里插入图片描述

方式四:使用Modelrequest域对象共享数据

本质和上面的ModelMap方式一样,需要在形参的位置传入ModelMap对象

案例

    // 使用ModelMap方式
    @RequestMapping("/modelMap")
    public String modeMap(ModelMap modelMap){
        modelMap.addAttribute("modelMap","使用ModelMap方式共享数据");
        return "/share/share";
    }

跳转到指定页面,并且在页面中获取request域中存储数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>共享数据</title>
    <style>
        * {
          margin: 0px auto;
        }
        .div {
            width: 500px;
        }
    </style>
</head>
<body>
<div class="div">
    <h1 style="color: red">SpringMVC常用三个共享域对象</h1>
    <h3 style="color: green" th:text="${servletAPI}"></h3>
    <h3 style="color: green" th:text="${modelAndView}"></h3>
    <h3 style="color: green" th:text="${map}"></h3>
    <h3 style="color: green" th:text="${model}"></h3>
    <h3 style="color: green" th:text="${modelMap}"></h3>
</div>
</body>
</html>

页面显示
在这里插入图片描述

Model ModelMap Map关系

Model ModelMap Map类型的参数其实本质上都是BindingAwareModelMap类型的,就是在形参位置上传入的其实就是BindingAwarModelMap类型

查看类的源码 :ctrl+H
在这里插入图片描述

在这里插入图片描述
上面的关系图,对应的代码

public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

BindingAwareModelMap源码

public class BindingAwareModelMap extends ExtendedModelMap {
    public BindingAwareModelMap() {
    }

    public Object put(String key, Object value) {
        this.removeBindingResultIfNecessary(key, value);
        return super.put(key, value);
    }

    public void putAll(Map<? extends String, ?> map) {
        map.forEach(this::removeBindingResultIfNecessary);
        super.putAll(map);
    }

    private void removeBindingResultIfNecessary(Object key, Object value) {
        if (key instanceof String) {
            String attributeName = (String)key;
            if (!attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
                String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attributeName;
                BindingResult bindingResult = (BindingResult)this.get(bindingResultKey);
                if (bindingResult != null && bindingResult.getTarget() != value) {
                    this.remove(bindingResultKey);
                }
            }
        }

    }
}

上面的ServletAPI原始方式 ModelAndView Model map ModelMap中的五种方式最终都是将数据和视图封装成ModelAndView对象

方式二:Session域对象

Session域对象中存储数据,使用是Servlet原始的中API,在控制器方法的形参位置传入HttpSession对象

使用ServletAPI一个Session对象,位置是在映射方法的形参位置传入HttpServletRequest对象,

使用HttpServletRequest对象获取一个Session对象

使用步骤是:

  1. 在控制器方法中的形参位置传入HttpServletRequest对象
  2. 使用HttpServletRequest对象获取一个Session对象
  3. 使用Session对象中的setAttribute方法向Session域中存储数据

案例

    // 向Session域对象储存数据,使用原始ServletAPI
    @RequestMapping("/session")
    public String session(HttpServletRequest request){
        // 获取HttpSession对象
        HttpSession session = request.getSession();
        session.setAttribute("sessionAPI","使用Session方式共享数据");
        return "/share/share";
    }
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>共享数据</title>
    <style>
        * {
          margin: 0px auto;
        }
        .div {
            width: 500px;
        }
    </style>
</head>
<body>
<div class="div">
    <h1 style="color: red">SpringMVC常用三个共享域对象</h1>
    <h3 style="color: green" th:text="${servletAPI}"></h3>
    <h3 style="color: green" th:text="${modelAndView}"></h3>
    <h3 style="color: green" th:text="${map}"></h3>
    <h3 style="color: green" th:text="${model}"></h3>
    <h3 style="color: green" th:text="${modelMap}"></h3>
    <h3 style="color: green" th:text="${session.sessionAPI}"></h3>
    <h3 style="color: green" th:text="${application.servletContext}"></h3>
</div>
</body>
</html>

在这里插入图片描述

方式三:向ServletContext域存储数据

application域存储共享数[其实就是ServletContext域对象]

使用是原始的ServletAPI获取ServletContext对象

可以使用HttpServeltRequest对象和HttpSession对象及PageContext都可以获取ServletContext对象

建议使用是HttpServeltRequest对象获取,就是在控制器方法的形参位置传入HttpServletRequest对象

使用步骤是:

  1. 在控制器方法中的形参位置传入HttpServletRequest对象
  2. 使用HttpServletRequest对象获取一个ServletContext对象
  3. 使用ServletContext对象中的setAttribute方法向application域中存储数据

案例:

    // 向Application域对象存储数据,使用原始ServletAPI
    @RequestMapping("/application")
    public String application(HttpServletRequest request){
        // 获取 ServletContext 对象
        ServletContext servletContext = request.getServletContext();
        // ServletContext域中存储数据
        servletContext.setAttribute("servletContext","使用ServletContext方式共享数据");
        return "/share/share";
    }
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>共享数据</title>
    <style>
        * {
          margin: 0px auto;
        }
        .div {
            width: 500px;
        }
    </style>
</head>
<body>
<div class="div">
    <h1 style="color: red">SpringMVC常用三个共享域对象</h1>
    <h3 style="color: green" th:text="${servletAPI}"></h3>
    <h3 style="color: green" th:text="${modelAndView}"></h3>
    <h3 style="color: green" th:text="${map}"></h3>
    <h3 style="color: green" th:text="${model}"></h3>
    <h3 style="color: green" th:text="${modelMap}"></h3>
    <h3 style="color: green" th:text="${session.sessionAPI}"></h3>
    <h3 style="color: green" th:text="${application.servletContext}"></h3>
</div>
</body>
</html>

在这里插入图片描述

总结

  1. Request存储数据建议使用ModelAndView对象
  2. Session存储数据建议使用HttpSession对象【就是ServletAPI原始方式】
  3. ServletContext存储数据建议使用HttpServletRequest对象【使用HttpServletRequest对象先获取ServeltContext对象,再向ServletContext域对象中共享数据】【就是ServletAPI原始方式】

可以理解为除了向Request域对象中存储数据,使用SpringMVC框架封装的ModelAndView对象,其他两种Session共享域对象和ServletContext共享域对象使用原生态的ServltAPI存储共享数据

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值