微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringMVC处理数据输出

一、 使用ModelAndVIew处理模型数据

控制器处理方法的返回值如果为ModelAndView, 则其既包含视图信息,也包含模型数据信息。数据是放在请求域中的。

    //使用ModelAndView
    @RequestMapping("/output3")
    public ModelAndView output3(){
        ModelAndView modelAndView = new ModelAndView("success");    //viewName即为跳转页面
        modelAndView.addobject("msg","ModelAndView处理数据");
        return modelAndView;
    }

二、使用Map处理模型数据

可以在方法的参数列表传入Map或者Model或者ModelMap,这些参数里面保存的所有数据都会放在request请求域中,可以在页面获取这些数据。

@Controller
public class OutputController {

    //使用Map
    @RequestMapping("/output")
    public String output(Map<String, Object> map){
        map.put("msg","输出数据处理");

        return "success";
    }

    //使用Model,一个接口
    @RequestMapping("/output1")
    public String output1(Model model){
        model.addAttribute("msg","model处理数据");

        return "success";
    }

    //使用ModelMap
    @RequestMapping("/output2")
    public String output2(ModelMap modelMap){
        modelMap.addAttribute("msg","modelMap处理数据");

        return "success";
    }
}

实际上Map、Model、ModelMap最终实现都是BindingAwareModelMap,相当于BindingAwareModelMap中保存的数据都会被放在请求域中。

在这里插入图片描述

Map是JDK中的一个interfaceModel是Spring中的一个interface,而ModelMap是Spring中的一个Class

ModelMap源码中实际上是继承LinkedHashMap类,所以本质上属于Map接口的一个实现类

public class ModelMap extends LinkedHashMap<String, Object>

BindingAwareModelMap源码中继承ExtendedModelMap类,而ExtendedModelMap这个类又继承于ModelMap类,同时实现Model接口。

public class BindingAwareModelMap extends ExtendedModelMap
public class ExtendedModelMap extends ModelMap implements Model 

所以Map、Model、ModelMap三者关系如下:

在这里插入图片描述

三、使用@SessionAttributes注解处理模型数据

SpringMVC提供了一种可以临时给Session域中保存数据的方式,即使用@SessionAttributes注解,这个注解只能作用在上。

//给BindingAwareModelMap中保存的数据,同时在session中也保存一份,value指定保存数据时要给session中放的数据的key
//type只要是指定的类型的数据,session就会保存
@SessionAttributes(value = "msg",types = {String.class})	
@Controller
public class OutputController

四、使用@modelattribute注解处理模型数据

某些业务场景不需要全字段更新,比如修改book对象信息,bookName只读而不能修改,只有其中某写字段的值可以修改。如果让SpringMVC去new一个对象,某些字段会有认值,将new出来的对象去更新数据库的值,很有可能会发生null覆盖了原来不能修改的字段的值。

所以,SpringMVC要封装请求参数的Book对象不应该是自己new出来的,而应该是从数据库中取出来的对象,使用这个对象来封装请求参数,这样只是修改了指定的字段值,没有修改的字段值保持原来的值。

@modelattribute注解可以加载参数上,也可以加在方法上,如果加在方法上,这个方法就会提前于目标方法运行。也就可以实现提前在数据库查询信息,并保存。在参数上加上注解就可以获取到这个从数据库中取出的对象,而不是去new一个对象出来。

@Controller
public class modelattributeTestController {

    private Object o1;
    private Object o2;

    @RequestMapping("/updateBook")
    public String updateBook(@modelattribute Book book, ModelMap modelMap){
        o2 = modelMap;
        System.out.println(o1 == o2);   //true
        
        //参数使用注解,就不会new一个对象,而是使用从数据库中取出的对象
        return "success";
    }


    @modelattribute
    public void modelAttr(Map<String, Object> map){
        o1 = map;
        //此方法数据库中取得数据,并提前于目标方法执行
    }
}

两个方法中的map以及book对象实际上都是同一个BindingAwareModelMap,实现的数据的互通。

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐