利用springmvc處理模型數(shù)據(jù)
springmvc處理模型數(shù)據(jù)
很多情況下頁面上需要很多數(shù)據(jù),單單返回頁面是不行的,那么springmvc如何將數(shù)據(jù)返回到該頁面呢
springmvc提供了四種方式來輸出模型數(shù)據(jù)
- ModelAndView: 處理返回值為ModelAndView時,可以將該對象中添加數(shù)據(jù)模型
- Map及Model:入?yún)镸odel、ModelMap或Map時,處理方法返回時,Map中的數(shù)據(jù)會自動添加到模型中
- @SessionAttributes: 將模型中的某個屬性暫存到HttpSession中,以便多個請求之間共享數(shù)據(jù)
- @ModelAttribute: 方法入?yún)?biāo)注該注解后,入?yún)⒌膶ο缶蜁诺綌?shù)據(jù)模型中
ModelAndView
主要有兩個重要的變量
// 視圖 可以傳字符串(視圖名字)也可以傳View對象 private Object view; // 數(shù)據(jù)模型 本質(zhì)是一個map private ModelMap model;
視圖相關(guān)的方法
// 設(shè)置視圖 public void setViewName(String viewName) { this.view = viewName; } // 獲取視圖 public String getViewName() { return this.view instanceof String ? (String)this.view : null; }
數(shù)據(jù)模型相關(guān)方法
// 獲取數(shù)據(jù)模型 protected Map<String, Object> getModelInternal() { return this.model; } public ModelMap getModelMap() { if (this.model == null) { this.model = new ModelMap(); } return this.model; } public Map<String, Object> getModel() { return this.getModelMap(); } // 添加視圖模型 public ModelAndView addObject(String attributeName, Object attributeValue) { this.getModelMap().addAttribute(attributeName, attributeValue); return this; }
springmvc底層使用request.setAttribute(name,value)來將數(shù)據(jù)放入到請求中
示例:
@RequestMapping("/modelAndViewTest") public ModelAndView modelAndViewTest(){ // 視圖名 ModelAndView modelAndView = new ModelAndView("modelAndViewTest"); // 包含的數(shù)據(jù) modelAndView.addObject("dateTime",new Date()); return modelAndView; }
Map及Model
@RequestMapping("/mapTest") public String mapTest(Map<String,String> map){ System.out.println(map.getClass()); //class org.springframework.validation.support.BindingAwareModelMap map.put("name","張三"); return "hello"; }
@SessionAttributes
在類上添加@SessionAttributes可以使該類所代表的路徑下的session共享
@Controller @RequestMapping("helloWorld") // 設(shè)置name屬性共享 @SessionAttributes(value={"name"}) public class HelloWorldController { @RequestMapping("/mapTest") public String mapTest(Map<String,String> map){ System.out.println(map.getClass()); //class org.springframework.validation.support.BindingAwareModelMap map.put("name","張三"); return "hello"; } // 可以在該方法中獲取到name值為張三 @RequestMapping("/sessionAttributes") public String sessionAttributes(HttpSession session){ System.out.println(session.getAttribute("name")); return "hello"; } }
@ModelAttribute
用在無返回值的方法
package com.yiidian.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class ModelAttributeController { //沒有返回值的情況 @ModelAttribute public void myModel(@RequestParam(required = false) String name, Model model) { model.addAttribute("name", name); } @RequestMapping(value = "/model") public String model() { return "success"; } }
用在帶返回值的方法
package com.yiidian.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class ModelAttributeController { /** * 帶返回值的情況 * @param name */ @ModelAttribute("name") public String myModel(@RequestParam(required = false) String name) { return name; } @RequestMapping(value = "/model") public String model() { return "success"; } }
應(yīng)用在方法的參數(shù)上
@ModelAttribute("name") public String myModel(@RequestParam(required = false) String name) { return name; } //應(yīng)用在方法的參數(shù)行 @RequestMapping(value = "/model") public String model(@ModelAttribute("name") String name) { System.out.println("name="+name); return "success"; }
以上就是利用springmvc 處理模型數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于springmvc 處理模型數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringMVC處理數(shù)據(jù)輸出的實例代碼
- Springmvc ResponseBody響應(yīng)json數(shù)據(jù)實現(xiàn)過程
- springMVC如何對輸入數(shù)據(jù)校驗實現(xiàn)代碼
- Springmvc如何返回xml及json格式數(shù)據(jù)
- SpringMvc響應(yīng)數(shù)據(jù)及結(jié)果視圖實現(xiàn)代碼
- Springmvc如何實現(xiàn)向前臺傳遞數(shù)據(jù)
- SPRINGMVC JSON數(shù)據(jù)交互如何實現(xiàn)
- SpringMVC數(shù)據(jù)輸出相關(guān)知識總結(jié)
相關(guān)文章
java基于netty NIO的簡單聊天室的實現(xiàn)
這篇文章主要介紹了java基于netty NIO的簡單聊天室的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效
這篇文章主要介紹了解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09遞歸出現(xiàn)棧溢出stackoverflow的問題及解決
這篇文章主要介紹了關(guān)于遞歸出現(xiàn)棧溢出stackoverflow的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09SpringBoot實現(xiàn)指標(biāo)監(jiān)控
這篇文章主要介紹了SpringBoot實現(xiàn)指標(biāo)監(jiān)控方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05基于spring實現(xiàn)websocket實時推送實例
這篇文章主要為大家詳細(xì)介紹了基于spring實現(xiàn)websocket實時推送實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03java javax.annotation.Resource注解的詳解
這篇文章主要介紹了javax.annotation.Resource注解的詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10