Piyush Mittal
Piyush Mittal

Reputation: 1890

diffrence between @RequestParam and @RequestMapping

Line1:

public ModelAndView viewCustomerDetails(@RequestParam("custId") Integer customerId, @RequestParam("categoryName") String categoryName, HttpServletRequest request) throws BusinessException{

Line2:

public ModelAndView viewCustomerDetails(@RequestMapping("custId") Integer customerId, @RequestMapping("categoryName") String categoryName, HttpServletRequest request) throws BusinessException{

i am going thorough my project code and having some confusion in @RequestParam and @RequestMapping some times i found @RequestParam and some times @RequestMapping. in my understanding both will assign the value of custId to customerId data member.

some part of my jsp file:

<form:input mandatory="true" id="CustNameTxt" path="custName" cssClass="txtfield controlWidth" readonly="false" />

for better understanding of my problem i have done editting in Line2

Upvotes: 0

Views: 3347

Answers (2)

Ralph
Ralph

Reputation: 120791

You compare apples with pears. Both annotations have nothing in common except that this are Spring MVC annotations, and your usage of @RequestMapping("categoryName") is wrong!

  • @RequestMapping is an class or method annotation to map the request url to the java method.
  • @RequestParam is a (Method) field annotation to to bind a request parameter to an method parameter

Maybe you mashed @RequestMapping with @PathVariable, and your question is about the difference of @RequestParam and @PathVariable - then have a look at this answer.

Upvotes: 6

Shahzeb
Shahzeb

Reputation: 4785

@RequestMapping maps the request to resource . It is used on methods not in their parameters .From SpringByExample

The @Controller indicates the class is a Spring MVC controller stereotype which is automatically registered by context:component-scan in the web-application-context.xml. The @RequestMapping annotation on the methods use the value attribute to map the method to a path. The method attribute is used to indicate the HTTP request type (ex: GET, POST, DELETE).

@RequestParam binds the parameters from resource URL to the arguments in the method.

Upvotes: 1

Related Questions