Answer the question
In order to leave comments, you need to log in
How to display separately information about a specific record from the spring boot database?
Good evening, Habr! There was a problem when writing an MVC application for working with a database (adding, deleting, editing, viewing). In general, the table is perfectly displayed separately, as you can see in the example
. I added the Details button to the table, which should show data about a particular client. Everything opens, the table is displayed, but, strangely, there is no data about the client
Below I will provide the code of the mustache file, the controller
@GetMapping(value = "/clients/{clientId}")
public String clientDetails(Model model, @PathVariable Integer clientId) {
Optional client = null;
try {
client = clientRepository.findById(clientId);
model.addAttribute("allowDelete", false);
} catch (Exception ex) {
model.addAttribute("errorMessage", ex.getMessage());
}
model.addAttribute("client", client);
return "client-details";
}
Answer the question
In order to leave comments, you need to log in
Good afternoon!
Problems can be in different places, both on the client side and on the server side.
Eliminate the following errors:
1) A valid GET request is sent from the client side with an id
/clients/{clientId}
. For example, /clients/6
2) In the controller, you receive it in the form @PathVariable Integer
Make sure that you have an integer, not a long, in your entity Client id. Otherwise, an exception will most likely be thrown.
3)
Optional client = null;
try {
client = clientRepository.findById(clientId);
model.addAttribute("allowDelete", false);
} catch (Exception ex) {
model.addAttribute("errorMessage", ex.getMessage());
}
model.addAttribute("client", client);
@GetMapping(value = "/clients/{clientId}")
public String clientDetails(Model model, @PathVariable Integer clientId) {
Optional client = clientRepository.findById(clientId).orElseThrow(ClientNotFoundException::new);
model.addAttribute("client", client);
return "client-details";
}
{{#clients}}
{{#client}}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question