P
P
postya2019-11-06 13:16:43
Spring
postya, 2019-11-06 13:16:43

How to get the data of one object from the database and apply it to normal html tags in Thymeleaf?

How to get the fields of a certain object by its id and display its separate field in thymeleaf, for example, in the tag ?
At the moment, I have all this displayed in a table that is in a form, but I do not want to have a form with a table, I would like to apply the received object fields to regular tags, for example, apply the data of the name field to the tag
I would like to have something in html something like this:
5dc29c439285c728272500.jpeg
there is an object model:

private int id;
  private int idFace;
  private String name;
  private String town;
  private String idType;
  private String post;
  private Date birthday;
  private String contacts;
  private String description;
  private String family;
  private String postIndex;
  private Long itn;

The service layer has a method for getting an object by its ID:
public Optional<Contact> get(int id) {
    return contactRepository.findById( id);
  }

The controller has a method for getting an object by id and sending the user to a specific url:
@GetMapping("/face/{id}")
  public ModelAndView editProductForm(@PathVariable(name = "id")int id) {
    ModelAndView mav = new ModelAndView("view_face");
    Optional contact = contactService.get(id);
    mav.addObject("contact", contact);
    return mav;
  }

in the view_face file there is such a structure:
<form action="#" th:object="${contact}" method="post">
                <table border="0" cellpadding="10">
                    <tr>
                        <td>Product ID:</td>
                        <td><input type="text" th:field="*{id}" readonly="readonly"></td>
                    </tr>
                    <tr>
                        <td>Product Name:</td>
                        <td><input type="text" th:field="*{name}"></td>
                    </tr>
                </table>
            </form>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question