@PathVariable doesn't work for only one of the mappings

949 views Asked by At

Hi everyone I usually use @PathVariable, but for this case, I don't know why it doesn't work.

menu.jsp:

<a href="<c:url value= '/admin/myCart/1/${idproducts}' />">Panier</a>      

Controller:

@RequestMapping(value = "/admin/viewProduct/{ref}", method = RequestMethod.GET)
public String getProduit(@PathVariable("ref") int ref, Model model) {
**model.addAttribute("idproducts",this.orderedProductService.getIdOrderedProductsByclient(1));**
model.addAttribute("produit", this.produitService.getProduitById(ref));


    return "menu";
}

@RequestMapping(value= "/admin/monPanier/{idproducts}", method = RequestMethod.GET)
public String myCart( @PathVariable("idproducts") String idproducts,Model model) {
     return mycart;
}

So menu.jsp get {idproducts} (which is a String containing ids separated by " / ", for example: idproducts = 5/6/7), but I don't know why @PathVariable("idproducts") doesn't work. The error is:

No mapping found for HTTP request with URI [/SpringMVCHibernate/admin/myCart/5/6/7/]

1

There are 1 answers

2
ScanQR On

You have URL defined as,

/admin/mycart/1/**${idproducts}**

But in controller there is no such mapping defined with /admin/mycart/1

It is defined to be with /admin/mycart,

@RequestMapping(value= "/admin/mycart/{idproducts}", method = RequestMethod.GET)
public String monPanier( @PathVariable("idproducts") String idproducts,Model model) {
     return mycart;
}

So try to fix your controller as follows,

@RequestMapping(value= "/admin/mycart/1/{idproducts}", method = RequestMethod.GET)
    public String monPanier( @PathVariable("idproducts") String idproducts,Model model) {
         return mycart;
}