SpringMVC處理數(shù)據(jù)輸出的實(shí)例代碼
一、 使用ModelAndVIew處理模型數(shù)據(jù)
控制器處理方法的返回值如果為ModelAndView, 則其既包含視圖信息,也包含模型數(shù)據(jù)信息。數(shù)據(jù)是放在請求域中的。
//使用ModelAndView
@RequestMapping("/output3")
public ModelAndView output3(){
ModelAndView modelAndView = new ModelAndView("success"); //viewName即為跳轉(zhuǎn)頁面
modelAndView.addObject("msg","ModelAndView處理數(shù)據(jù)");
return modelAndView;
}
二、使用Map處理模型數(shù)據(jù)
可以在方法的參數(shù)列表傳入Map或者M(jìn)odel或者M(jìn)odelMap,這些參數(shù)里面保存的所有數(shù)據(jù)都會放在request請求域中,可以在頁面中獲取這些數(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ù)都會被放在請求域中。

Map是JDK中的一個(gè)interface,Model是Spring中的一個(gè)interface,而ModelMap是Spring中的一個(gè)Class
ModelMap源碼中實(shí)際上是繼承LinkedHashMap類,所以本質(zhì)上屬于Map接口的一個(gè)實(shí)現(xiàn)類
public class ModelMap extends LinkedHashMap<String, Object>
BindingAwareModelMap源碼中繼承ExtendedModelMap類,而ExtendedModelMap這個(gè)類又繼承于ModelMap類,同時(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è)注解只能作用在類上。
//給BindingAwareModelMap中保存的數(shù)據(jù),同時(shí)在session中也保存一份,value指定保存數(shù)據(jù)時(shí)要給session中放的數(shù)據(jù)的key
//type只要是指定的類型的數(shù)據(jù),session就會保存
@SessionAttributes(value = "msg",types = {String.class})
@Controller
public class OutputController
四、使用@ModelAttribute注解處理模型數(shù)據(jù)
某些業(yè)務(wù)場景不需要全字段更新,比如修改book對象信息,bookName只讀而不能修改,只有其中某寫字段的值可以修改。如果讓SpringMVC去new一個(gè)對象,某些字段會有默認(rèn)值,將new出來的對象去更新數(shù)據(jù)庫的值,很有可能會發(fā)生null值覆蓋了原來不能修改的字段的值。
所以,SpringMVC要封裝請求參數(shù)的Book對象不應(yīng)該是自己new出來的,而應(yīng)該是從數(shù)據(jù)庫中取出來的對象,使用這個(gè)對象來封裝請求參數(shù),這樣只是修改了指定的字段值,沒有修改的字段值保持原來的值。
@ModelAttribute注解可以加載參數(shù)上,也可以加在方法上,如果加在方法上,這個(gè)方法就會提前于目標(biāo)方法運(yùn)行。也就可以實(shí)現(xiàn)提前在數(shù)據(jù)庫查詢信息,并保存。在參數(shù)上加上注解就可以獲取到這個(gè)從數(shù)據(jù)庫中取出的對象,而不是去new一個(gè)對象出來。
@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ù)使用注解,就不會new一個(gè)對象,而是使用從數(shù)據(jù)庫中取出的對象
return "success";
}
@ModelAttribute
public void modelAttr(Map<String, Object> map){
o1 = map;
//此方法從數(shù)據(jù)庫中取得數(shù)據(jù),并提前于目標(biāo)方法執(zhí)行
}
}
兩個(gè)方法中的map以及book對象實(shí)際上都是同一個(gè)BindingAwareModelMap,實(shí)現(xiàn)的數(shù)據(jù)的互通。

總結(jié)
到此這篇關(guān)于SpringMVC處理數(shù)據(jù)輸出的文章就介紹到這了,更多相關(guān)SpringMVC處理數(shù)據(jù)輸出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 利用springmvc處理模型數(shù)據(jù)
- Springmvc ResponseBody響應(yīng)json數(shù)據(jù)實(shí)現(xiàn)過程
- springMVC如何對輸入數(shù)據(jù)校驗(yàn)實(shí)現(xiàn)代碼
- Springmvc如何返回xml及json格式數(shù)據(jù)
- SpringMvc響應(yīng)數(shù)據(jù)及結(jié)果視圖實(shí)現(xiàn)代碼
- Springmvc如何實(shí)現(xiàn)向前臺傳遞數(shù)據(jù)
- SPRINGMVC JSON數(shù)據(jù)交互如何實(shí)現(xiàn)
- SpringMVC數(shù)據(jù)輸出相關(guān)知識總結(jié)
相關(guān)文章
java累加和校驗(yàn)實(shí)現(xiàn)方式16進(jìn)制(推薦)
下面小編就為大家?guī)硪黄猨ava累加和校驗(yàn)實(shí)現(xiàn)方式16進(jìn)制(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
Java中ArrayBlockingQueue和LinkedBlockingQueue
這篇文章主要介紹了Java中ArrayBlockingQueue和LinkedBlockingQueue,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09
SpringBoot v2.2以上重復(fù)讀取Request Body內(nèi)容的解決方案
這篇文章主要介紹了SpringBoot v2.2以上重復(fù)讀取Request Body內(nèi)容的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
java實(shí)現(xiàn)excel導(dǎo)入數(shù)據(jù)的工具類
這篇文章主要介紹了java實(shí)現(xiàn)的excel導(dǎo)入數(shù)據(jù)的工具類,需要的朋友可以參考下2014-03-03
Intellij IDEA如何去掉@Autowired 注入警告的方法
這篇文章主要介紹了Intellij IDEA如何去掉@Autowired 注入警告的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
詳解MyBatis直接執(zhí)行SQL查詢及數(shù)據(jù)批量插入
這篇文章主要介紹了MyBatis直接執(zhí)行SQL查詢及數(shù)據(jù)批量插入的相關(guān)知識,需要的朋友一起學(xué)習(xí)吧2016-01-01

