Java 如何使用Thymeleaf将元素添加到集合中
在我的Spring/Thymeleaf/Hibernate项目中,我需要将元素添加到编辑对象的字段(即列表)中。既然不允许嵌入,我该怎么做? 诸如此类:Java 如何使用Thymeleaf将元素添加到集合中,java,html,spring,thymeleaf,Java,Html,Spring,Thymeleaf,在我的Spring/Thymeleaf/Hibernate项目中,我需要将元素添加到编辑对象的字段(即列表)中。既然不允许嵌入,我该怎么做? 诸如此类: @Entity public class Recipe { @Id private int id; private String name; private String description; private List<String> steps; } @实体 公共课食谱{ @身份证
@Entity
public class Recipe {
@Id
private int id;
private String name;
private String description;
private List<String> steps;
}
@实体
公共课食谱{
@身份证
私有int-id;
私有字符串名称;
私有字符串描述;
私有列表步骤;
}
和我的ThymileAF模板:
<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 -->
如何使其不丢失已编辑的配方对象数据?
使用
和
添加?
还是仅仅使用
?那么,怎样才能不丢失输入的数据呢?好吧,找到了解决方案,当然不理想,但它是有效的。
在我的类中添加了字段(它将是标志,我们应该在表单操作url的端点中做什么:将步骤添加到列表并重定向到再次编辑或只是保存。表单有2个提交按钮:并且,通过JavaScrypt将标志字段更改为“步骤”,所以我知道我已经将元素添加到列表并重定向回。
JavaScrypt:
<script type="text/javascript">
function toggleAddedStep()
{
document.getElementById("recipeAddedFlag").value = "step";
}
function toggleAddedNothing()
{
document.getElementById("recipeAddedFlag").value = "";
}
</script>
函数togleAddedStep()
{
document.getElementById(“RecipeDedFlag”).value=“步骤”;
}
函数togleAddedNothing()
{
document.getElementById(“RecipeDedFlag”).value=“”;
}
嗯,找到了解决方案,当然不理想,但它确实有效。
在我的类中添加了字段(它将是标志,我们应该在表单操作url的端点中做什么:将步骤添加到列表并重定向到再次编辑或只是保存。表单有2个提交按钮:并且,通过JavaScrypt将标志字段更改为“步骤”,所以我知道我已经将元素添加到列表并重定向回。
JavaScrypt:
<script type="text/javascript">
function toggleAddedStep()
{
document.getElementById("recipeAddedFlag").value = "step";
}
function toggleAddedNothing()
{
document.getElementById("recipeAddedFlag").value = "";
}
</script>
函数togleAddedStep()
{
document.getElementById(“RecipeDedFlag”).value=“步骤”;
}
函数togleAddedNothing()
{
document.getElementById(“RecipeDedFlag”).value=“”;
}