0

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

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39
  • `@RequestParam` map a single request parameter, `@RequestMapping` should be used to map a web resource to the handler class or method not to map parameters – fantarama Jul 14 '15 at 05:50

2 Answers2

6

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.

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • thanks @Ralph for dropping ur valuable comment yah ´@RequestMapping("categoryName")´ was wrong. i've already mentioned it in my problem that i've editted Line2 to explain my problem in a better way. – Piyush Mittal Jul 14 '15 at 06:11
1

@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.

Shahzeb
  • 4,745
  • 4
  • 27
  • 40