在我的Spring / Thymeleaf / Hibernate项目中,我需要能够将元素添加到我的编辑对象的字段(这是List<>)。我是如何制作的,因为不允许使用嵌入式? 这样的事情:
@Entity
public class Recipe {
@Id
private int id;
private String name;
private String description;
private List<String> steps;
}
我的百里叶模板:
<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 -->
如何制作,而不是丢失Recipe对象的已编辑数据?
使用<form>
和<button type="submit">
添加?
或者只是使用<a href>
?那么如何丢失输入的数据呢?
答案 0 :(得分:0)
嗯,找到解决方案,肯定不理想,但它确实有效。 我的类添加了字段(它将是标志,我们应该在form-action-url的端点做什么:将步骤添加到List并重定向再次编辑或只保存。并且表单有2个提交按钮:并且,将标志字段更改为&#34;步骤&#34;由JavaScrypt,所以我知道我已经添加元素到List并重定向回来。 JavaScrypt:
<script type="text/javascript">
function toggleAddedStep()
{
document.getElementById("recipeAddedFlag").value = "step";
}
function toggleAddedNothing()
{
document.getElementById("recipeAddedFlag").value = "";
}
</script>