html input with date type binding java attribute date

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

html input with date type binding java attribute date

idamien
Hello,


I use <input type="date" in my html page,  this input box is mapped to the attribut date of my java bean Bean.

When i click on the submit button,  i got a white label page error.  If i replace type ="date" by type="texte", and then i type the date manual, everything goes fine.  U have the code below.

Any idea ?  


transactionForm.html  :

<!DOCTYPE html>
<html>
<head>
<title>Add a transaction</title>
<link rel="stylesheet" href="/bootstrap-4.0.0/css/bootstrap.min.css" />
</head>
<body>
       

Add a transaction

        <div class="container">
                <form action="#" th:action="@{/transactions/add}" th:object="${transaction}"
                        method="post">
                        <div class="form-group">
                                <label for="transactionDate">Transaction Date :</label>
                                <input id="transactionDate" class="form-control" type="date" th:field="*{date}" />
                        </div>
                        <button type="submit" class="btn btn-primary">Submit</button>
                </form>
        </div>
</body>
</html>

Class Java Transaction :

@Entity
public class Transaction {
        @Id
        @GeneratedValue
        private Long id;

        @Column
        @DateTimeFormat(pattern = "dd/MM/yyyy")
        private Date date;

.....
}

Controller  :

        @GetMapping("/transactions/add")
        public String transactionForm(Model model) {
                model.addAttribute("transaction", new Transaction());
                return "transactionForm";
        }

        @PostMapping("/transactions/add")
        public String createTransaction(@ModelAttribute Transaction transaction) {
               
                transactionRepository.saveAndFlush(transaction);
               
                return "redirect:/transactions";
        }