我有一个Expense类,它有一个ExpenseType类型的字段expenseType。我想在视图页面上显示Expense和ExpenseType类的所有字段。我用每个标签显示ExpenseType中的所有字段,因为它是Expense类中的列表。但是这些字段没有在视图页面上呈现。
在下面的代码中,不显示每个标签内的标签名称和价格。我尝试使用放置在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>
我的控制器:
@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;
这些字段未显示的可能错误是什么。
答案 0 :(得分:2)
在模型中添加属性时,即
model.addAttribute("expenses", new Expenses());
您尚未设置Bean费用的expenseType字段,您只需使用新的费用()。
而是尝试将字段expenseType初始化为新的ArrayList()。即
Expenses exp = new Expenses();
exp.setExpensesType(new ArrayList());
model.addAttribute("expenses", exp);