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

利用springmvc處理模型數(shù)據(jù)

 更新時(shí)間:2021年03月05日 09:15:45   作者:小菜鳥  
這篇文章主要介紹了如何利用springmvc 處理模型數(shù)據(jù),幫助大家更好的理解和學(xué)習(xí)使用springmvc,感興趣的朋友可以了解下

springmvc處理模型數(shù)據(jù)

很多情況下頁面上需要很多數(shù)據(jù),單單返回頁面是不行的,那么springmvc如何將數(shù)據(jù)返回到該頁面呢

springmvc提供了四種方式來輸出模型數(shù)據(jù)

  • ModelAndView: 處理返回值為ModelAndView時(shí),可以將該對(duì)象中添加數(shù)據(jù)模型
  • Map及Model:入?yún)镸odel、ModelMap或Map時(shí),處理方法返回時(shí),Map中的數(shù)據(jù)會(huì)自動(dòng)添加到模型中
  • @SessionAttributes: 將模型中的某個(gè)屬性暫存到HttpSession中,以便多個(gè)請(qǐng)求之間共享數(shù)據(jù)
  • @ModelAttribute: 方法入?yún)?biāo)注該注解后,入?yún)⒌膶?duì)象就會(huì)放到數(shù)據(jù)模型中

ModelAndView

主要有兩個(gè)重要的變量

// 視圖 可以傳字符串(視圖名字)也可以傳View對(duì)象
private Object view;
// 數(shù)據(jù)模型 本質(zhì)是一個(gè)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ù)放入到請(qǐng)求中

示例:

@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ù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java基于netty NIO的簡(jiǎn)單聊天室的實(shí)現(xiàn)

    java基于netty NIO的簡(jiǎn)單聊天室的實(shí)現(xiàn)

    這篇文章主要介紹了java基于netty NIO的簡(jiǎn)單聊天室的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 解決IDEA maven 項(xiàng)目修改代碼不生效,mvn clean、install后才生效

    解決IDEA maven 項(xiàng)目修改代碼不生效,mvn clean、install后才生效

    這篇文章主要介紹了解決IDEA maven 項(xiàng)目修改代碼不生效,mvn clean、install后才生效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • ReentrantLock獲取鎖釋放鎖的流程示例分析

    ReentrantLock獲取鎖釋放鎖的流程示例分析

    這篇文章主要為大家介紹了ReentrantLock獲取鎖釋放鎖的流程示例分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • RabbitMQ消息隊(duì)列的目錄結(jié)構(gòu)

    RabbitMQ消息隊(duì)列的目錄結(jié)構(gòu)

    這篇文章主要介紹了RabbitMQ消息隊(duì)列的目錄結(jié)構(gòu),RabbitMQ?屬于消息中間件,主要用于組件之間的解耦,消息的發(fā)送者無需知道消息使用者的存在,反之亦然,那么用了那么久RabbitMQ,其目錄結(jié)構(gòu)是怎樣的呢,讓我們一起來看一下吧
    2023-08-08
  • 基于mybatis一對(duì)多查詢內(nèi)層排序的問題

    基于mybatis一對(duì)多查詢內(nèi)層排序的問題

    這篇文章主要介紹了基于mybatis一對(duì)多查詢內(nèi)層排序的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • 用java WebSocket做一個(gè)聊天室

    用java WebSocket做一個(gè)聊天室

    這篇文章主要為大家詳細(xì)介紹了用java WebSocket做一個(gè)聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 遞歸出現(xiàn)棧溢出stackoverflow的問題及解決

    遞歸出現(xiàn)棧溢出stackoverflow的問題及解決

    這篇文章主要介紹了關(guān)于遞歸出現(xiàn)棧溢出stackoverflow的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控

    SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 基于spring實(shí)現(xiàn)websocket實(shí)時(shí)推送實(shí)例

    基于spring實(shí)現(xiàn)websocket實(shí)時(shí)推送實(shí)例

    這篇文章主要為大家詳細(xì)介紹了基于spring實(shí)現(xiàn)websocket實(shí)時(shí)推送實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • java javax.annotation.Resource注解的詳解

    java javax.annotation.Resource注解的詳解

    這篇文章主要介紹了javax.annotation.Resource注解的詳解的相關(guān)資料,需要的朋友可以參考下
    2016-10-10

最新評(píng)論