我有一个 Expense 类,它有一个 ExpenseType 列表类型的字段expenseType。我想在 View 页面上显示 Expense 和 ExpenseType 类中的所有字段。我使用每个标签来显示 ExpenseType 中的所有字段,因为它是 Expense 类中的列表。但这些字段没有呈现在 View 页面上。
在下面的代码中,不显示每个标签内的标签名称和价格。我尝试使用放置在 foreach 标记旁边的 c:out 标记显示一些值,但它从未显示,这意味着它在 c:forEach 处失败
这是我的jsp代码:
<form:form method="POST" modelAttribute="expenses" action="saveExpense">
<div class="box-body">
<div class="form-group col-xs-6 text-spacing">
<label>Expense Date</label>
<form:input path="date" id="expenseDate" class="form-control"/>
<form:errors path="date" cssClass="error" />
</div>
<div class="form-group col-xs-6 text-spacing">
<label>Shop/Dealer Name</label>
<form:input path="shopName" type="text" class="form-control" placeholder="dealer name"/>
<form:errors path="shopName" cssClass="error" />
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div data-role="dynamic-fields">
<div class="form-inline">
<c:forEach items="${expenses.expensesType}" varStatus="status">
<div class="form-group">
<label class="sr-only" for="field-name">Name</label>
<form:input path="expensesType[${status.index}].name" type="text"/>
</div>
<span>-</span>
<div class="form-group">
<label class="sr-only" for="field-value">Price</label>
<form:input path="expensesType[${status.index}].price" type="text"/>
</div>
<button class="btn btn-danger" data-role="remove">
<span class="glyphicon glyphicon-remove"></span>
</button>
<button class="btn btn-primary" data-role="add">
<span class="glyphicon glyphicon-plus"></span>
</button>
</c:forEach>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
<a class="btn btn-primary" href="<c:url value='expensesList'/>"> Cancel </a>
</div>
</form:form>
我的 Controller :
@RequestMapping(value = "/expensesForm", method = RequestMethod.GET)
public String getForm(Model model) {
model.addAttribute("expenses", new Expenses());
return "expensesForm";
}
@RequestMapping(value = "/saveExpense", method = RequestMethod.POST)
public String saveCattleDetails(@Valid @ModelAttribute("expenses") Expenses expenses, BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
return "expensesForm";
}
expensesService.add(expenses);
return "redirect:expensesList";
}
模型类
public class Expenses implements Serializable {
private static final long serialVersionUID = 668608674926007321L;
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "EXPENSES_ID", unique = true)
private String id;
@NotNull
@Column(name = "DATE")
private Date date;
@Column(name = "SHOP_NAME")
private String shopName;
@Column(name = "TOTAL_EXPENSE")
private String totalExpenses;
@Column(name = "DESCRIPTION")
private String description;
@OneToMany(mappedBy = "expenses", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<ExpensesType> expensesType;
和
@Entity
@Table(name = "EXPENSES_TYPE")
public class ExpensesType implements Serializable {
private static final long serialVersionUID = 3002107481242774411L;
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "EXPENSE_TYPE_ID", unique = true)
private String id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "EXPENSES_ID")
private Expenses expenses;
@Column(name = "NAME")
private String name;
@Column(name = "CATEGORY")
private CategoryType category;
@Column(name = "PRICE")
private String price;
未显示这些字段可能会出现什么错误。
最佳答案
当您在模型中添加属性时,即
model.addAttribute("expenses", new Expenses());
您还没有设置 bean Expenses 的费用类型字段,您只是使用了 new Expenses()。
尝试将字段expenseType 初始化为new ArrayList()。即
Expenses exp = new Expenses();
exp.setExpensesType(new ArrayList());
model.addAttribute("expenses", exp);
关于spring-mvc - 在 Spring 中循环 JSTL 表单标签内的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35878254/