程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> springmvc學習筆記(15)-數據回顯

springmvc學習筆記(15)-數據回顯

編輯:JAVA綜合教程

springmvc學習筆記(15)-數據回顯


springmvc學習筆記(15)-數據回顯


本文介紹springmvc中數據回顯的幾種實現方法

數據回顯:提交後,如果出現錯誤,將剛才提交的數據回顯到剛才的提交頁面。

pojo數據回顯方法

1.springmvc默認對pojo數據進行回顯。

pojo數據傳入controller方法後,springmvc自動將pojo數據放到request域,key等於pojo類型(首字母小寫)

使用@ModelAttribute指定pojo回顯到頁面在request中的key

2.@ModelAttribute還可以將方法的返回值傳到頁面

在商品查詢列表頁面,通過商品類型查詢商品信息。在controller中定義商品類型查詢方法,最終將商品類型傳到頁面。

 // 商品分類
//itemtypes表示最終將方法返回值放在request中的key
@ModelAttribute("itemtypes")
public Map getItemTypes() {

    Map itemTypes = new HashMap();
    itemTypes.put("101", "數碼");
    itemTypes.put("102", "母嬰");

    return itemTypes;
}

頁面上可以得到itemTypes數據。


    商品名稱:
    商品類型:
    

3.使用最簡單方法使用model,可以不用@ModelAttribute

//可以直接使用model將提交pojo回顯到頁面
//model.addAttribute("items", itemsCustom);

簡單類型數據回顯

使用最簡單方法使用model

model.addAttribute("id", id);

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved