A
Spring is making a mistake, because you're referring to and trying to open a page that is not being processed by any of the methods you've brought on this issue.Why is that what happened and what we're gonna do next?@RequestParam@RequestParam is an annotation that intercepts the request parameters.Exactly. PARAMETR OBJECTIVE and nothing else.That's what comes after the question mark in the request line.http://localhost:8888/home?param1=test¶m2=value
In this case, we could intercept these data through the following annotations and parameters.@RequestParam("param1") String firstParameter, @RequestParam("param2") String secondParameter
Which means it's not what we need, because with the annotations we can't get the data out of the way... (sighs)before question)For these things, variable paths And we'll talk about them later.@GetMappingAnnotation @GetMapping links the queries corresponding to the template to the method to be processed.In this case, annotations @GetMapping(value = "/item") allows us to process species requests. http://localhost:8888/item or at best http://localhost:8888/shop/item (sighs)As long as you've still indicated mapping over the controller himself.)And that's not what we need again.Based on all references, request that processes the method itemPage() Your controller would look like this:http://localhost:8888/item?shop=123&item=456
orhttp://localhost:8888/shop/item?shop=123&item=456
(subject to annotations with a counter-eller)You're expecting:http://localhost:8888/shop/123/item/456
Now let's figure out how to achieve this.@PathVariableAs I mentioned, you needed to use variable paths.Annotations @PathVariable enables the counter-residential parameter to be linked to the part of the template to must satisfy the request.To this end, the pathways use palesholders limited to the right and left of the figure boxes.For example:@GetMapping(value="/schools/{school}/courses/{course}/materials")
Two players are listed in the annotations {school} and {course}which we can then intercept by annotations, as follows:@PathVariable("school") Long schoolId, @PathVariable("course") Long courseId,
In your case, this couple will look as follows::Shablon:@GetMapping(value = "/shop/{shop}/item/{item}")
or@GetMapping(value = "/{shop}/item/{item}")
If the counter-eller is marked with a template ("/shop)and parameters:@PathVariable("shop") Long shopId, @PathVariable("item") Long itemId,
Outcome:// метод shopPage() в данном листинге не указываю, т.к. он без изменений
// далее
@GetMapping(value = "/shop/{shop}/item/{item}")
public String itemPage(Model model, @PathVariable("shop") Long shopId, @PathVariable("item") Long itemId, Principal principal) {
try {
String username = principal.getName();
if (username != null) {
User user = userService.findByLogin(username);
if (!user.isFieldsNotNull()) {
return "redirect:/" + username;
}
model.addAttribute("sign", "выйти");
}
} catch (Exception e) {
model.addAttribute("sign", "войти");
}
Item item = shopService.getItemById(shopService.findById(shopId).getItems(), itemId);
List<ImageDto> images = imageMapper.toListImageDto(item.getImages());
model.addAttribute("item", itemMapper.itemToItemDTO(item));
model.addAttribute("image", shopService.convertListImages(images));
return "shop_item_page";
}