In my Spring/Thymeleaf/Hibernate project I need ability add element to field (which is List<>) of my editing object. How I an make it, since embedded are not allowed?
Something like that:
@Entity
public class Recipe {
@Id
private int id;
private String name;
private String description;
private List<String> steps;
}
And my thymeleaf template:
<form th:action="@{|/save/${recipe.id}|}" method="post" th:object="${recipe}">
<input type="hidden" th:field="${recipe.id}" th:value="${recipe.id}"/>
<input type="text" th:field="${recipe.name}" th:value="${recipe.name}"/>
<input type="text" th:field="${recipe.description}" th:value="${recipe.description}"/>
<input th:each="step : ${recipe.steps}" type="text" th:field="${step}" th:value="${step}"/>
<!-- And here I want make ability add new step element to List<String> steps -->
How to make it, not losing already edited data of Recipe object?
Add using <form>
and <button type="submit">
?
Or just using <a href>
? How can I not lose entered data then?
Well, found solution, sure not ideal, but it works. Added field to my class (it will be flag, what we should do in endpoint of form-action-url: add step to List and redirect to edit again or just save. And form have 2 submit buttons: and , changes flag field to "step" by JavaScrypt, so I will know I have add element to List and redirect back. JavaScrypt:
<script type="text/javascript">
function toggleAddedStep()
{
document.getElementById("recipeAddedFlag").value = "step";
}
function toggleAddedNothing()
{
document.getElementById("recipeAddedFlag").value = "";
}
</script>