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

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

標籤: springmvcjava


[TOC]git


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

數據回顯:提交後,若是出現錯誤,將剛纔提交的數據回顯到剛纔的提交頁面。spring

pojo數據回顯方法

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

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

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

2.@ModelAttribute還能夠將方法的返回值傳到頁面網站

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

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

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

    return itemTypes;
}

頁面上能夠獲得itemTypes數據。code

<td>
    商品名稱:<input name="itemsCustom.name" />
    商品類型:
    <select name="itemtype">
        <c:forEach items="${itemtypes}" var="itemtype">
            <option value="${itemtype.key }">${itemtype.value }</option>
        </c:forEach>
    </select>
</td>

3.使用最簡單方法使用model,能夠不用@ModelAttribute

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

簡單類型數據回顯

使用最簡單方法使用model

model.addAttribute("id", id);


做者@brianway更多文章:我的網站 | CSDN | oschina

相關文章
相關標籤/搜索