I am new to Spring MVC - and I am trying to pass a date from my javascript as a request Param
My controller looks something like -
public @ResponseBody List<RecordDisplay> getRecords( @RequestParam(value="userID") Long userID, @RequestParam(value="fromDate") Date fromDate, @RequestParam(value="toDate") Date toDate) {
The question I have is how do I make the call from javascript - as in what should the URL look like
for eg. - /getRecords?userID=1&fromDate=06022013&toDate=08022013'
Do I need a way to parse the date so Spring can recognize it?
Convert Date Parameters on Request Level DATE) Date date) { // ... } @PostMapping("/local-date") public void localDate(@RequestParam("localDate") @DateTimeFormat(iso = DateTimeFormat. ISO. DATE) LocalDate localDate) { // ... }
set('currentdate', moment(). format(("YYYY-MM-DD"))); {{$timestamp}} -> predefined variable gets the current timestamp, Since you need date the above one should work. use the parameter {{currentdate}} in your GET request.
Call the format() method of DateFormat class and pass the date object as a parameter to the method.
Difference between @PathVariable and @RequestParam in Spring 1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.
Use @DateTimeFormat("MMddyyyy")
public @ResponseBody List<RecordDisplay> getRecords( @RequestParam(value="userID") Long userID, @RequestParam(value="fromDate") @DateTimeFormat(pattern="MMddyyyy") Date fromDate, @RequestParam(value="toDate") @DateTimeFormat(pattern="MMddyyyy") Date toDate) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With