I have a couple of methods that process requests through Spring MVC.

They look like this:

@RequestMapping(value = "/update/{id}", method = RequestMethod.GET) public String update(@PathVariable("id") int id, Model model) { Meal meal = get(id); //отдельный метод model.addAttribute("meal", meal); return "mealForm"; } @RequestMapping(value = "/update", method = RequestMethod.POST) public String update(HttpServletRequest request) { int userId = AuthorizedUser.id(); int id = Integer.parseInt(request.getParameter("id")); Meal meal = new Meal(); meal.setId(id); meal.setUser(new User()); meal.setCalories(Integer.parseInt(request.getParameter("calories"))); meal.setDescription(request.getParameter("description")); meal.setDateTime(LocalDateTime.parse(request.getParameter("dateTime"))); log.info("update {} with id={} for userId={}", meal, id, userId); assureIdConsistent(meal, id); service.update(meal, userId); return "meals"; } 

When calling a link

 <td><a href="update/${meal.id}">Update</a></td> 

processes the @RequestMapping(value = "/update/{id}", method = RequestMethod.GET) method @RequestMapping(value = "/update/{id}", method = RequestMethod.GET) , then generates a View with a form for changing parameters, sends it to http://localhost:8081/topjava/update/100007 and click Save to send a POST request to the @RequestMapping(value = "/update", method = RequestMethod.POST) method @RequestMapping(value = "/update", method = RequestMethod.POST) . The problem is that the expected address

 http://localhost:8081/topjava/update 

(which will then be processed by the second method), and it turns out

 http://localhost:8081/topjava/update/update 

that is, an extra /update is added with an error 405 (does not find the method).

How can I overcome this problem and remove the extra /update ?

    2 answers 2

     <a href="${pageContext.request.contextPath}/update/${meal.id}">Update</a> 
    • Thank you, Sergey! - Vyacheslav Chernyshov

    I think it's enough to put a slash before the update in the link address so that the address is not perceived as relative:

     <a href="/update/${meal.id}">Update</a>