You are on page 1of 3

Note: annotation hiu l ghi ch, ch thch

Mapping: nh x

Mapping Request with @RequestMapping

- Chng ta s dng @RequestMapping annotation map URL v nh


/appointments vo mt class hoc cc phng thc x l ring bit ca class
. Thng th annotation ca 1 class s cho bit mt phn ca path-ng
dn c reqqest cho mt lp controller c th , thm na cc Annotation cho
cc menthod phi ch r phng thc request l GET hay POST.

-Trong v d trn @RequestMapping c s dng mt s v tr, Trc ht n


c s dng mc class-level ch rn tt c menthod trong class nhn v x
l cc request c path bt u l /appoinments. Phng thc get() ch chp
nhn reqquest GET tc l vi cc li gi requesst ti controller c action l
GET v path l /appoitments th phng thc GET() s c gi x l.
Phng thc getNewForm() c gi khi c request menthod=GET v
path=/appointmennts/new.
URI Template Patterns
Mt URL template ging nh mt chui string cha mt hoc nhiu tn bin, khi
bn thay i value ca cc bin , template tr thnh mt URL, v d cho mt
mu URL template sau : http://www.example.com/users/{userId} cha bin
userId. Gn cho bin value l fred ta c URL
http://www.example.com/users/fred .

- Trong Srping MVC bn c th s dng @PathVariable annotation l mt


tham s ca menthod (menthod argument) bind-kt ni value ca mt
URL template variable (nm na l ly ci gi tr ca ci bin trong mu
URL thng qua bin @PathVariable)

Ta thy Url mu /owners/{ownersId} cha bin tn ownersId Khi controller x


l request ny gi tr ca tham s ownersId s c gn cho gi tr tng
ng trong Url mu request ti. v d request ti l /owners/fred th value ca
ownersId l fred.

x l @PathVariable annotation Spring MVC cn tm kim URL template


variable ph hp theo name, Bn c th c t n trong annotation:

Hoc nu URL template variable ng vi menthod argument name bn c th b


qua chi tit
Mt method c th c nhiu @PathVariable annotation:

You might also like