Using parameters in @RequestMapping

We have seen how to use the @RequestMapping annotation to map the incoming request to a Controller method. So far, we have mapped static URL patterns in @RequestMapping. However, it is possible to map parameterized URLs (like those used in REST; see https://spring.io/understanding/REST) using @RequestMapping. The parameters are specified inside {}.

Let's add the feature to update an existing course. Here, we will only discuss how to code the Controller method for this feature. The complete code is available when you download the samples for this chapter.

Let's add the following method in CourseController:

  @RequestMapping("/course/update/{id}") 
  public String updateCourse (@PathVariable int id, Model model) { 
    //TODO: Error handling 
    CourseDTO course = coursesDAO.getCourse(id); 
    model.addAttribute("course", course); 
    model.addAttribute("title", "Update Course"); 
    return "updateCourse"; 
  } 

Here, we map the updateCourse method to handle requests with the following URL pattern: /course/update/{id}, where {id} could be replaced with the ID (number) of any existing course, or for that matter, any integer. To access the value of this parameter, we used the @PathVariable annotation in the arguments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.72.15