2010-11-01 16 views
13

arasındaki fark nedir? Örneğin, @PathVariable ve @RequestParam arasındaki fark nedir?Bir Spring 3.0 GET isteğinde, @PathVariable ve @RequestParam

@RequestMapping(value = "/portfolio/{portfolioIdPath}", method = RequestMethod.GET) 
public final String portfolio(HttpServletRequest request, ModelMap model, 
@PathVariable long portfolioIdPath, @RequestParam long portfolioIdRequest) 

cevap

17

@RequestParam senin yönteminde bir parametreye bir istek parametresini bağlar. senin örnekte, istek in "portfolioIdRequest" adlı parametrenin değeri geçilecek yönteminize "portfolioIdRequest" argümanı olarak. Daha somut bir örnek - istek URL'si sonra

http://hostname/portfolio/123?portfolioIdRequest=456 

parametresi "portfolioIdRequest" değeri ise "456" olacaktır. Burada

diğer bilgiler: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

@PathVariable benzer yöntem parametresi "portfolioIdPath" URI şablon değişkeni "portfolioIdPath" değerini bağlar. Örneğin, eğer URI

/portfolio/123 

ardından "portfolioIdPath" yöntemi parametresinin değeri "123" olacaktır.

buradadiğer bilgiler: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates

2

@RequestParam istemci (kullanıcı) ile gönderilen HTTP GET veya POST parametre oluşturmaktadır, ve @ RequestMapping isteğe isteğe değişir URL bir bölümünü çıkartır In:

http://host/?var=1 

Yukarıdaki URL "var" bir istek mektubudur.

http://host/registration/{which} 

ve üzeri

URL {hangi} bir istek haritalama olduğunu. Sen hizmetinizi diyebiliriz:

http://host/registration/user 

VEYA uygulamasında

http://host/registration/firm 

gibi değerini erişebilir {hangi} (ikinci yılında "kullanıcı" = ve İlk durumda hangi = "firması".

0

Bu sizin istek işlemek istediğiniz şekilde bağlıdır

@RequestParam example 
(request)http://something.com/owner?ownerId=1234 

@PathVariable example 
(request) http://something.com/owner/1234 
(in tour code) /owner/{ownerId} 
İlgili konular