利用springmvc處理模型數據
更新時間:2021年03月05日 09:15:45 作者:小菜鳥
這篇文章主要介紹了如何利用springmvc 處理模型數據,幫助大家更好的理解和學習使用springmvc,感興趣的朋友可以了解下
springmvc處理模型數據
很多情況下頁面上需要很多數據,單單返回頁面是不行的,那么springmvc如何將數據返回到該頁面呢
springmvc提供了四種方式來輸出模型數據
- ModelAndView: 處理返回值為ModelAndView時,可以將該對象中添加數據模型
- Map及Model:入參為Model、ModelMap或Map時,處理方法返回時,Map中的數據會自動添加到模型中
- @SessionAttributes: 將模型中的某個屬性暫存到HttpSession中,以便多個請求之間共享數據
- @ModelAttribute: 方法入參標注該注解后,入參的對象就會放到數據模型中
ModelAndView
主要有兩個重要的變量
// 視圖 可以傳字符串(視圖名字)也可以傳View對象 private Object view; // 數據模型 本質是一個map private ModelMap model;
視圖相關的方法
// 設置視圖 public void setViewName(String viewName) { this.view = viewName; } // 獲取視圖 public String getViewName() { return this.view instanceof String ? (String)this.view : null; }
數據模型相關方法
// 獲取數據模型 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)來將數據放入到請求中
示例:
@RequestMapping("/modelAndViewTest") public ModelAndView modelAndViewTest(){ // 視圖名 ModelAndView modelAndView = new ModelAndView("modelAndViewTest"); // 包含的數據 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") // 設置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"; } }
應用在方法的參數上
@ModelAttribute("name") public String myModel(@RequestParam(required = false) String name) { return name; } //應用在方法的參數行 @RequestMapping(value = "/model") public String model(@ModelAttribute("name") String name) { System.out.println("name="+name); return "success"; }
以上就是利用springmvc 處理模型數據的詳細內容,更多關于springmvc 處理模型數據的資料請關注腳本之家其它相關文章!
相關文章
解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效
這篇文章主要介紹了解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09java javax.annotation.Resource注解的詳解
這篇文章主要介紹了javax.annotation.Resource注解的詳解的相關資料,需要的朋友可以參考下2016-10-10