如何获取Localdate作为参数?

时间:2018-07-15 11:33:16

标签: java spring-mvc thymeleaf

我有一个看起来像这样的表格:

<form th:action="@{/viewExpenses}" method="post" class="form-horizontal">
    <div class="form-group">
        <label for="date" class="control-label col-md-3">Date:-</label>
        <div class="col-md-9">
            <input type="date" class="form-control" id="date" th:name="date"/>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-3 col-md-4">
            <button class="btn btn-primary">View Details</button>
        </div>
    </div>
</form>

现在,当我提交此表单时,我希望将日期值设为LocalDate。所以在我的控制器上,我做到了:

@RequestMapping(value = "/viewExpenses", method = RequestMethod.POST)
public String expenseDetails(Model model, @RequestParam("date")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {

    BigDecimal totalExpense = new BigDecimal(0);

    List<Expenditure> e = expenditureService.viewExpenseDetails(date);

    for (Expenditure exp : e) {
        totalExpense = totalExpense.add(exp.getAmount());
    }

    model.addAttribute("showExpenditure", true);
    model.addAttribute("totalExpense", totalExpense);
    model.addAttribute("expenses", e);
    return "finance/expensedetails";
}

现在,我在控制器中获得的日期值为null。我曾尝试更改@DateTimeFormat的模式,但仍然无法使用。
接收日期参数的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

这似乎没有绑定。您可以尝试使用spring:form标记并通过modelAttribute绑定控制器对象。