Как отправить данные через POST thymeleaf form без формы ввода данных
Пишу программку, где клиент при входе получает список предложений и может выбрать одно из них. Данные на страницу передаются списком через model.addAttribute("offers", viewOffers)
<table class="table table-sm table-hover align-middle">
<thead>
<tr>
<th scope="col">сумма(руб.)</th>
<th scope="col">Проценты(%)</th>
<th scope="col">Срок(лет)</th>
</tr>
</thead>
<tbody>
<th:block th:each="viewOffer : ${offers}">
<tr>
<td th:text="${viewOffer.sum}">сумма</td>
<td th:text="${viewOffer.percentage}">процент</td>
<td th:text="${viewOffer.term}">срок</td>
<td>
<div class="d-grid gap-2 d-md-block">
<form action="#" th:action="@{/service/offerDetails}" th:object="${viewOffer}" method="post">
<input type="hidden" th:value="${viewOffer.sum}" />
<input type="hidden" th:value="${viewOffer.percentage}" />
<input type="hidden" th:value="${viewOffer.term}" />
<button type="submit" name="submit" value="Подробнее">Подробнее</button>
</form>
</div>
</td>
</tr>
</th:block>
</tbody>
Модель совсем простая.
@NoArgsConstructor
public class ViewOffer {
public int sum;
public double percentage;
public int term;
}
Данные на странице отображаются, но при нажатии кнопки они не передаются в @PostMapping(объект с пустыми значениями).
@PostMapping(value = "/offerDetails")
public String issueCredit(@ModelAttribute ViewOffer viewOffer, Model model)
С thymeleaf дружу плохо. Все примеры в интернете описывают поля для ввода, а у меня по сути полей нет, а уже готовые данные. Буду рад любому ответу. Спасибо.
Ответы (2 шт):
Автор решения: Владлен Вожжаев
→ Ссылка
<table class="table table-sm table-hover align-middle">
<thead>
<tr>
<th scope="col">сумма(руб.)</th>
<th scope="col">Проценты(%)</th>
<th scope="col">Срок(лет)</th>
</tr>
</thead>
<tbody>
<th:block th:each="viewOffer : ${offers}">
<tr>
<td th:text="${viewOffer.sum}">сумма</td>
<td th:text="${viewOffer.percentage}">процент</td>
<td th:text="${viewOffer.term}">срок</td>
<td>
<div class="d-grid gap-2 d-md-block">
<form action="#" th:action="@{/service/offerDetails}" th:object="${viewOffer}" method="post">
<input name="sum" type="hidden" th:value="${viewOffer.sum}" />
<input name="percentage" type="hidden" th:value="${viewOffer.percentage}" />
<input name="term" type="hidden" th:value="${viewOffer.term}" />
<button type="submit" name="submit" value="Подробнее">Подробнее</button>
</form>
</div>
</td>
</tr>
</th:block>
</tbody>
Автор решения: Яблочко
→ Ссылка
Нашел решение. В модель добавляем гетеры и сеттеры в саму форму в input добавляем id
<table class="table table-sm table-hover align-middle">
<thead>
<tr>
<th scope="col">сумма(руб.)</th>
<th scope="col">Проценты(%)</th>
<th scope="col">Срок(лет)</th>
</tr>
</thead>
<tbody>
<th:block th:each="viewOffer : ${offers}">
<form action="#" th:action="@{/service/offerDetails}" th:object="${viewOffer}" method="post">
<tr>
<td th:text="${viewOffer.sum}">сумма</td>
<td th:text="${viewOffer.percentage}">процент</td>
<td th:text="${viewOffer.term}">срок</td>
<td>
<div class="d-grid gap-2 d-md-block">
<input name="sum" id="sum" th:value="${viewOffer.sum}" type="hidden"/>
<input name="percentage" id="percentage" th:value="${viewOffer.percentage}" type="hidden"/>
<input name="term" id="term" th:value="${viewOffer.term}" type="hidden"/>
<button type="submit" value="Подробнее">Подробнее</button>
</div>
</td>
</tr>
</form>
</th:block>
</tbody>