L
L
Leo Sobolev2021-05-08 18:43:28
PostgreSQL
Leo Sobolev, 2021-05-08 18:43:28

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
6096b0798d485043450231.jpeg

. 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
6096b0cae7cd2907505332.jpeg

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";
}


6096b13dc7e6a259509223.jpeg

I'm just wildly sorry for the screenshot of the code, I know that this is not done, I just haven't fully figured out how to make questions in Habré yet :)
I'm a newbie!
Sorry for the stupid question, I just started to understand and learn web development.
Thanks in advance for your reply!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2021-05-08
@azerphoenix

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);

This part of the code can be simplified. For example,
@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";
    }

But I suspect that you have a banal typo.
In the template you use:
{{#clients}}
And in the controller you add - client.
Which means it should be:
{{#client}}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question