SpringMVC数据存储

本文介绍了SpringMVC中数据存储的各种方式,包括Model、ModelMap、ModelAndView、Map、Request和Session。ModelMap作为数据容器,类似request.setAttribute。ModelAndView用于设置视图和传递数据。@SessionAttributes注解提供临时保存Session数据的功能。在redirect跳转后,SpringMVC通过RedirectAttributesModelMap保持数据。建议根据需求选择合适的数据存储方法。
摘要由CSDN通过智能技术生成

SpringMVC数据存储

1、Model

Model是一个接口,其实现类为ExtendedModelMap,继承了ModelMap类。

public class ExtendedModelMap extends ModelMap implements Model

它的addAttribute方法,会使用到ModelMap的addAttribute方法,ModelMap又会调用到继承自LinkedHashMap的put方法

public ExtendedModelMap addAttribute(String attributeName, @Nullable Object attributeValue) {
   
        super.addAttribute(attributeName, attributeValue);
        return this;
    }

2、ModelMap

ModelMap是一个类,继承LinkedHashMap,因此为Map结构,可以使用Key/Value形式存储值。

  • LinkedHashMap简单来说是一个有序的HashMap,其是HashMap的子类,HashMap是无序的。
public class ModelMap extends LinkedHashMap<String, Object>

ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可,
他的作用类似于request对象的setAttribute方法的作用: 用来在一个请求过程中传递处理的数据

它的addAttribute方法,底层调用的是Map的put方法

public ModelMap addAttribute(String attributeName, @Nullable Object attributeValue) {
   
        Assert.notNull
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值