SpringMVC数据存储

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(attributeName, "Model attribute name must not be null");
        this.put(attributeName, attributeValue);
        return this;
    }

3、ModelAndView

添加模型数据用addObject;
设置视图setViewName;
ModelAndView 对象有两个作用:
(1). 设置转向地址,这也是ModelAndView和ModelMap的主要区别.设置方式如下所示:

ModelAndView view = new ModelAndView("path:student");

或者通过setViewName方式:

public void setViewName(String viewName){...}

(2). 将控制器方法中处理的结构数据传递到jsp页面,把数据放到ModelAndView对象中其实底层是把数据放入到了ModelMap中

public ModelAndView addObject(String attributeName, Object attributeValue) {
        this.getModelMap().addAttribute(attributeName, attributeValue);
        return this;
    }
public ModelMap getModelMap() {
        if (this.model == null) {
            this.model = new ModelMap();
        }

4、Map

@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
    map.put("name","张三");
    map.put("password","123456");
    map.put("age",18);
    return "success";
}

5、Request

使用request域对象存储数据:将请求中的参数存储在request中,使用setAttribute()方法

@RequestMapping("/testRequest")
    public String testRequest(HttpServletRequest request) {
        request.setAttribute("name", "张三");
        return "success";
    }

6、Session

将数据存储到session作用域中

@RequestMapping("/testSession")
    public String testSession(HttpSession session) {
        session.setAttribute("name", "张三");
        return "success";
    }

Model、ModelMap、ModelAndView、Map是SpringMVC中特有的,Request、Session是servlet的API,是通用的。

7、@SessionAttributes

SpringMVC提供了一种可以临时给Session域中保存数据的方式,那就是使用@SessionAttributes注解,但该注解只能标注在类上
使用@SessionAttributes(value = “msg”)注解会在给BindingAwareModelMap中保存数据的同时,为session中存放一份。value指定保存数据时要给session中存放的key。

  1. value = {“msg”}:只要保存的是这种key的数据,给session中存放一份。
  2. types={String.class}:只要保存的是这种类型的数据,给session中存放一份。

SpringMVC虽然提供了为session存放数据的@SessionAttributes注解,不过还是推荐使用原始API,因为使用SpringMVC提供的注解可能会引发异常。

SpringMVC Redirect 跳转后保存Model 中的数据

  1. 手动拼接url

    return "redirect:list-user.shtml?param1="+value1+"&param2="+value2;
    

    这个方式比较麻烦而且有个弊端,就是参数是中文的时候很难处理

  2. 自己封装一个类

    自己进行一些封装,包括中文的处理,转码解码等,好处是可以根据自己想要的自由实现,坏处是增加了工作量。在一些没有提供现成工具的框架中适合用此方法。

  3. 使用spring mvc提供的现成的工具类

    spring mvc中,我们常用的是ModelMap,但是它还提供了一个RedirectAttributesModelMap类,该类实现了RedirectAttributes接口,提供一个闪存存储方案,使属性能够在重定向时依旧生存而不用嵌入到url

    public class RedirectAttributesModelMap extends ModelMap implements RedirectAttributes 
        
    public interface RedirectAttributes extends Model
    
    @RequestMapping(value = "delete-user", method = RequestMethod.POST)
    public String deleteUser(Long[] userId, RedirectAttributesModelMap modelMap) {
    userService.deleteUser(userId);
    modelMap.addFlashAttribute("resultMsg", "删除成功");
    return "redirect:list-user.shtml";
    }
    

    发现进行redirect跳转后,“删除成功”的消息仍旧为我们保持着。

    其实最底层仍旧是两种跳转,只不过spring又进行了封装而已,原理是把属性放到session中,在跳到页面后又在session中马上移除对象,所以在刷新一下后这个值就会丢掉。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值