javascript
SpringMVC:学习笔记(4)——处理模型数据
SpringMVC—處理模型數(shù)據(jù)
說明
SpringMVC?提供了以下幾種途徑輸出模型數(shù)據(jù):
–?ModelAndView:?處理方法返回值類型為?ModelAndView時(shí),?方法體即可通過該對(duì)象添加模型數(shù)據(jù)
–?Map及Model:入?yún)閛rg.springframework.ui.Model、org.springframework.ui.ModelMap?或?Java.uti.Map?時(shí),處理方法返回時(shí),Map中的數(shù)據(jù)會(huì)自動(dòng)添加到模型中。
–?@SessionAttributes:?將模型中的某個(gè)屬性暫存到HttpSession?中,以便多個(gè)請(qǐng)求之間可以共享這個(gè)屬性
–?@ModelAttribute:?方法入?yún)?biāo)注該注解后,?入?yún)⒌膶?duì)象就會(huì)放到數(shù)據(jù)模型中。
ModelAndView
說明
一旦Controller處理完客戶請(qǐng)求,則返回ModelAndView對(duì)象給DispatcherServlet前端控制器。ModelAndView中包含了模型(Model)和視圖(View)。從宏觀角度考慮,DispatcherServlet是整個(gè)Web應(yīng)用的控制器;從微觀角度考慮,Controller是單個(gè)Http請(qǐng)求處理過程中的控制器,而ModelAndView是Http請(qǐng)求過程中返回的模型和視圖。
即,控制器處理方法的返回值如果為ModelAndView,則其中既包含視圖信息,也包含模型數(shù)據(jù)信息。
示例
1.創(chuàng)建ModelAndView,并傳入模型數(shù)據(jù)
2.編寫JSP頁面,進(jìn)行測(cè)試
說明:SpringMVC會(huì)把ModelAndView中的Model中的數(shù)據(jù)方法Request域?qū)ο笾小!?/span>
Map及Model
說明:?
Spring MVC 在內(nèi)部使用了一個(gè)org.springframework.ui.Model 接口存儲(chǔ)模型數(shù)據(jù)。
Spring MVC 在調(diào)用方法前會(huì)創(chuàng)建一個(gè)隱含的模型對(duì)象作為模型數(shù)據(jù)的存儲(chǔ)容器。
如果方法的入?yún)?Map 或 Model 類型,Spring MVC 會(huì)將隱含模型的引用傳遞給這些入?yún)?/span>。在方法體內(nèi),開發(fā)者可以通過這個(gè)入?yún)?duì)象訪問到模型中的所有數(shù)據(jù),也可以向模型中添加新的屬性數(shù)據(jù)。
示例:
使用MODEL:
這里傳入的Map實(shí)際為BindingAwareModelMap,所以我們定義參數(shù)用Map即可。
?
說明:
并且在這里我們也是可以使用Model類型的。
@SessionAttribute
若希望在多個(gè)請(qǐng)求之間共用某個(gè)模型屬性數(shù)據(jù),則可以在控制器類上標(biāo)注一個(gè) @SessionAttributes,Spring MVC將在模型中對(duì)應(yīng)的屬性暫存到 HttpSession 中。
說明:
注意這個(gè)注解只能放到類的上面
1.首先使用Map將模型數(shù)據(jù)存到請(qǐng)求域中,然后在類定義處使用@SessionAttributes,拷貝到Session中。
2.編寫JSP頁面及測(cè)試
補(bǔ)充
@SessionAttributes 除了可以通過屬性名指定需要放到會(huì)話中的屬性外,還可以通過模型屬性的對(duì)象類型指定哪些模型屬性需要放到會(huì)話中
– @SessionAttributes(types=User.class) 會(huì)將隱含模型中所有類型為 User.class 的屬性添加到會(huì)話中。
– @SessionAttributes(value={“user1”, “user2”})
– @SessionAttributes(types={User.class, Dept.class})
– @SessionAttributes(value={“user1”, “user2”},types={Dept.class})
@ModelAttribute
說明:
前面談到SpringMVC在每次調(diào)用請(qǐng)求處理方法時(shí),都會(huì)創(chuàng)建Model類型的一個(gè)實(shí)例。如果準(zhǔn)備使用此實(shí)例,則可以在方法中添加一個(gè)Model類型的參數(shù)。還可以使用在方法中添加@ModelAttribute注釋類型來訪問Model實(shí)例。
可以用@ModelAttribute來注釋方法參數(shù):帶有@ModelAttribute注解的方法會(huì)將其輸入或創(chuàng)建的參數(shù)對(duì)象添加到Model對(duì)象中(若方法中沒有顯式添加)。
可以用@ModelAttribute標(biāo)注一個(gè)非請(qǐng)求的處理方法:被@ModelAttribute注釋的方法會(huì)在此controller每個(gè)方法執(zhí)行前被執(zhí)行。
基本用法:
1.在方法上使用@ModelAttribute
@Controllerpublic class UserDao{.....@ModelAttributepublic User addUser(User user){return new User(201702,"MrSaber");}.....}說明:
@ModelAttribute 注解的方法可以返回一個(gè) 對(duì)象或者一個(gè)void類型。如果返回一個(gè)對(duì)象則默認(rèn)添加到Model中。若方法返回一個(gè)void類型,則還必須自行將實(shí)例添加到Model中。
?
@Controllerpublic class UserDao{.....@ModelAttributepublic void addUser(User user,Model model){...model.addAttribute(user);}.....}2.在方法參數(shù)上使用@ModelAttribute.
@RequestMapping("/save") public String save(@ModelAttribute("newUser") User user) { user.setUsername("U love me"); userService.save(user); return "result"; }說明:
輸入或創(chuàng)建的User實(shí)例將用newUser鍵值添加到Model對(duì)象中。如果未定義將默認(rèn)用小寫名稱作為鍵。
轉(zhuǎn)載于:https://www.cnblogs.com/MrSaver/p/6395684.html
總結(jié)
以上是生活随笔為你收集整理的SpringMVC:学习笔记(4)——处理模型数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IP分类以及特殊IP
- 下一篇: Nginx的配置文件nginx.conf