欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC處理數(shù)據(jù)輸出的實(shí)例代碼

 更新時(shí)間:2021年05月02日 11:13:05   作者:煎丶包  
這篇文章主要給大家介紹了關(guān)于SpringMVC處理數(shù)據(jù)輸出的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、 使用ModelAndVIew處理模型數(shù)據(jù)

控制器處理方法的返回值如果為ModelAndView, 則其既包含視圖信息,也包含模型數(shù)據(jù)信息。數(shù)據(jù)是放在請(qǐng)求域中的。

    //使用ModelAndView
    @RequestMapping("/output3")
    public ModelAndView output3(){
        ModelAndView modelAndView = new ModelAndView("success");    //viewName即為跳轉(zhuǎn)頁(yè)面
        modelAndView.addObject("msg","ModelAndView處理數(shù)據(jù)");
        return modelAndView;
    }

二、使用Map處理模型數(shù)據(jù)

可以在方法的參數(shù)列表傳入Map或者M(jìn)odel或者M(jìn)odelMap,這些參數(shù)里面保存的所有數(shù)據(jù)都會(huì)放在request請(qǐng)求域中,可以在頁(yè)面中獲取這些數(shù)據(jù)。

@Controller
public class OutputController {

    //使用Map
    @RequestMapping("/output")
    public String output(Map<String, Object> map){
        map.put("msg","輸出數(shù)據(jù)處理");

        return "success";
    }

    //使用Model,一個(gè)接口
    @RequestMapping("/output1")
    public String output1(Model model){
        model.addAttribute("msg","model處理數(shù)據(jù)");

        return "success";
    }

    //使用ModelMap
    @RequestMapping("/output2")
    public String output2(ModelMap modelMap){
        modelMap.addAttribute("msg","modelMap處理數(shù)據(jù)");

        return "success";
    }
}

實(shí)際上Map、Model、ModelMap最終實(shí)現(xiàn)都是BindingAwareModelMap,相當(dāng)于BindingAwareModelMap中保存的數(shù)據(jù)都會(huì)被放在請(qǐng)求域中。

Map是JDK中的一個(gè)interface,Model是Spring中的一個(gè)interface,而ModelMap是Spring中的一個(gè)Class

ModelMap源碼中實(shí)際上是繼承LinkedHashMap類(lèi),所以本質(zhì)上屬于Map接口的一個(gè)實(shí)現(xiàn)類(lèi)

public class ModelMap extends LinkedHashMap<String, Object>

BindingAwareModelMap源碼中繼承ExtendedModelMap類(lèi),而ExtendedModelMap這個(gè)類(lèi)又繼承于ModelMap類(lèi),同時(shí)實(shí)現(xiàn)Model接口。

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

所以Map、Model、ModelMap三者關(guān)系如下:

三、使用@SessionAttributes注解處理模型數(shù)據(jù)

SpringMVC提供了一種可以臨時(shí)給Session域中保存數(shù)據(jù)的方式,即使用@SessionAttributes注解,這個(gè)注解只能作用在類(lèi)上。

//給BindingAwareModelMap中保存的數(shù)據(jù),同時(shí)在session中也保存一份,value指定保存數(shù)據(jù)時(shí)要給session中放的數(shù)據(jù)的key
//type只要是指定的類(lèi)型的數(shù)據(jù),session就會(huì)保存
@SessionAttributes(value = "msg",types = {String.class}) 
@Controller
public class OutputController

四、使用@ModelAttribute注解處理模型數(shù)據(jù)

某些業(yè)務(wù)場(chǎng)景不需要全字段更新,比如修改book對(duì)象信息,bookName只讀而不能修改,只有其中某寫(xiě)字段的值可以修改。如果讓SpringMVC去new一個(gè)對(duì)象,某些字段會(huì)有默認(rèn)值,將new出來(lái)的對(duì)象去更新數(shù)據(jù)庫(kù)的值,很有可能會(huì)發(fā)生null值覆蓋了原來(lái)不能修改的字段的值。

所以,SpringMVC要封裝請(qǐng)求參數(shù)的Book對(duì)象不應(yīng)該是自己new出來(lái)的,而應(yīng)該是從數(shù)據(jù)庫(kù)中取出來(lái)的對(duì)象,使用這個(gè)對(duì)象來(lái)封裝請(qǐng)求參數(shù),這樣只是修改了指定的字段值,沒(méi)有修改的字段值保持原來(lái)的值。

@ModelAttribute注解可以加載參數(shù)上,也可以加在方法上,如果加在方法上,這個(gè)方法就會(huì)提前于目標(biāo)方法運(yùn)行。也就可以實(shí)現(xiàn)提前在數(shù)據(jù)庫(kù)查詢(xún)信息,并保存。在參數(shù)上加上注解就可以獲取到這個(gè)從數(shù)據(jù)庫(kù)中取出的對(duì)象,而不是去new一個(gè)對(duì)象出來(lái)。

@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
        
        //參數(shù)使用注解,就不會(huì)new一個(gè)對(duì)象,而是使用從數(shù)據(jù)庫(kù)中取出的對(duì)象
        return "success";
    }


    @ModelAttribute
    public void modelAttr(Map<String, Object> map){
        o1 = map;
        //此方法從數(shù)據(jù)庫(kù)中取得數(shù)據(jù),并提前于目標(biāo)方法執(zhí)行
    }
}

兩個(gè)方法中的map以及book對(duì)象實(shí)際上都是同一個(gè)BindingAwareModelMap,實(shí)現(xiàn)的數(shù)據(jù)的互通。

總結(jié)

到此這篇關(guān)于SpringMVC處理數(shù)據(jù)輸出的文章就介紹到這了,更多相關(guān)SpringMVC處理數(shù)據(jù)輸出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論