Spring mvc 中model.addAttribute("student",student)請問這句是什麼意思
阿新 • • 發佈:2018-12-13
按照spring一般的編碼習慣,model 應該是contrller裡面的Map結構吧。Map裡面新增key=“student”,value=“student物件”的意思,最後把這個model返回一個jsp,在jsp頁面上就能得到這個student物件了!
freemarker 整合springmvc時, ModelMap物件的 addAttribute,put兩個方法有什麼區別
這個是 原始碼中 ModelMap的定義 類 public class ModelMap extends LinkedHashMap<String, Object> 說明 ModelMap是繼承自LinkedHashMap的,則put方法是繼承自 HashMap的方法,沒什麼特殊 而addAttribute方法的定義 public ModelMap addAttribute(String attributeName, Object attributeValue) { Assert.notNull(attributeName, "Model attribute name must not be null"); put(attributeName, attributeValue); return this; } 其實也是呼叫的put方法,但是會在呼叫之前判斷 key值是否為null,如果為null則會報錯 java.lang.IllegalArgumentException: Model attribute name must not be null,而put方法不會檢查key值是否會空 綜上,則 ModelMap物件的 addAttribute,put兩個方法有什麼區別就是 addAttribute是不允許新增空值的key,put是允許的