[@controller]4 詳解@modelattribute_第1頁(yè)
已閱讀1頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、A、@ModelAttributeAnnotationthatbindsamethodparametermethodreturnvaluetoanamedmodelattributeexposedtoawebview.SupptedfRequestMappingannotatedhlerclasses.在被@RequestMapping注釋的處理器類中,這個(gè)注釋可以綁定一個(gè)方法參數(shù)或綁定一個(gè)方法的返回值到一個(gè)命名的模型屬性,提供給一個(gè)視

2、圖。CanbeusedtoexposecommobjectstoawebviewusingspecificattributenamesthroughannotatingcrespondingparametersofaRequestMappingannotatedhlermethod).可以用于把一個(gè)comm對(duì)象提供給web視圖,使用指定的屬性名稱,在被@RequestMapping注釋的處理器方法中注釋相關(guān)參數(shù)。Canalsobeuse

3、dtoexposereferencedatatoawebviewthroughannotatingaccessmethodsinacontrollerclasswhichisbasedonRequestMappingannotatedhlermethodswithsuchaccessmethodsallowedtohaveanyargumentsthatRequestMappingsupptsfhlermethodsreturningt

4、hemodelattributevaluetoexpose.可以用于提供數(shù)據(jù)給一個(gè)web視圖,通過注釋處理器方法,這個(gè)方法允許有任何參數(shù),返回的模型屬性值被提供。A.1、@ModelAttribute的屬性的屬性valueThenameofthemodelattributetobindto.綁定的模型屬性的名稱。Thedefaultmodelattributenameisinferredfromthedeclaredattributet

5、ype(i.e.themethodparametertypemethodreturntype)basedonthenonqualifiedclassname:e.g.“derAddress“fclass“mypackage.derAddress““derAddressList“f“List“.默認(rèn)的模型屬性名稱自動(dòng)判斷聲明的屬性類型(如,方法參數(shù)類型或方法返回類型)。如這個(gè)值是derAddress,就對(duì)于當(dāng)前包.derAddress。B

6、、@ModelAttribute注釋一個(gè)方法注釋一個(gè)方法An@ModelAttributeonamethodindicatesthepurposeofthatmethodistoaddonememodelattributes.Suchmethodssupptthesameargumenttypesas@RequestMappingmethodsbutcannotbemappeddirectlytorequests.Instead@Mod

7、elAttributemethodsinacontrollerareinvokedbefe@RequestMappingmethodswithinthesamecontroller.被@ModelAttribute注釋的方法表示這個(gè)方法的目的是增加一個(gè)或多個(gè)模型(model)屬性。這個(gè)方法和被@RequestMapping注釋的方法一樣也支持@RequestParam參數(shù),但是它不能直接被請(qǐng)求映射。實(shí)際上,控制器中的@ModelAttr

8、ibute方法是在同一控制器中的@RequestMapping方法被調(diào)用之前調(diào)用的。@ModelAttributemethodsareusedtopopulatethemodelwithcommonlyneededattributesfexampletofilladownwithstateswithpettypestoretrieveacommobjectlikeAccountindertouseittorepresentthedata

9、onanHTMLfm.被@ModelAttribute注釋的方法用于填充model屬性,例如,為下拉菜單填充內(nèi)容,或檢索一個(gè)comm對(duì)象(如,Account),用它來(lái)表示一個(gè)HTML表單中的數(shù)據(jù)。Acontrollercanhaveanynumberof@ModelAttributemethods.Allsuchmethodsareinvokedbefe@RequestMappingmethodsofthesamecontroller.

10、一個(gè)控制器可以有任意數(shù)量的@ModelAttribute方法。所有這些方法都在@RequestMapping方法被調(diào)用之前調(diào)用。publicStringhelloWld()return“helloWld“這個(gè)例子中使用@ModelAttribute注釋的value屬性,來(lái)指定model屬性的名稱。model屬性對(duì)象就是方法的返回值。它無(wú)須要特定的參數(shù)。B.4、@ModelAttribute和@RequestMapping同時(shí)注釋一個(gè)方法

11、同時(shí)注釋一個(gè)方法舉例說(shuō)明舉例說(shuō)明@ControllerpublicclassHelloWldController@RequestMapping(value=“helloWld.do“)@ModelAttribute(“attributeName“)publicStringhelloWld()return“hi“這時(shí)這個(gè)方法的返回值并不是表示一個(gè)視圖名稱,而是model屬性的值,視圖名稱由RequestToViewNameTranslat

12、根據(jù)請(qǐng)求“helloWld.do“轉(zhuǎn)換為helloWld。Model屬性名稱有@ModelAttribute(value=””)指定。C、@ModelAttribute注釋一個(gè)方法的參數(shù)注釋一個(gè)方法的參數(shù)An@ModelAttributeonamethodargumentindicatestheargumentshouldberetrievedfromthemodel.Ifnotpresentinthemodelthearguments

13、houldbeinstantiatedfirstthenaddedtothemodel.Oncepresentinthemodeltheargumentsfieldsshouldbepopulatedfromallrequestparametersthathavematchingnames.ThisisknownasdatabindinginSpringMVCaveryusefulmechanismthatsavesyoufromhav

14、ingtoparseeachfmfieldindividually.@ModelAttribute注釋方法的一個(gè)參數(shù)表示應(yīng)從模型model中取得。若在model中未找到,那么這個(gè)參數(shù)將先被實(shí)例化后加入到model中。若在model中找到,則請(qǐng)求參數(shù)名稱和model屬性字段若相匹配就會(huì)自動(dòng)填充。這個(gè)機(jī)制對(duì)于表單提交數(shù)據(jù)綁定到對(duì)象屬性上很有效。B.1、從、從model中獲取中獲取Itmayalreadybeinthemodelduetoan

15、@ModelAttributemethodinthesamecontroller參數(shù)的值從當(dāng)前控制器的@ModelAttribute方法提供的model屬性中獲取。舉例說(shuō)明舉例說(shuō)明@ControllerpublicclassHelloWldController@ModelAttribute(“user“)publicUseraddAccount()returnnewUser(“jz““123“)@RequestMapping(value

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論