'How to return Thymeleaf page with sent Date param
I need directions how to solve issue with CRUD app. I have model class that have Date field:
@Column(name = "date", nullable = false)
@DateTimeFormat(pattern = "dd.MM.yyyy.")
private Date date;
In Thymeleaf template date field is put in textfield that using JQuery datepicker. That part working as intended.
<form th:action="@{/poetracker/addNew}" th:object="${item}" method="post" style="max-width:418px; margin:0 auto">
<div class="pt-3">
<div class="form-group row">
<label for="datepicker" class="col-sm-4 col-form-label">Датум:</label>
<div class="col-sm-8">
<input type="text" id="datepicker" th:field="*{date}" readonly autocomplete="off" class="form-control form-control-sm"/>
</div>
//some other components rendering
</div>
</form>
Now comes the part I have trouble with. When submit button is clicked, item added to database, I want form to be reloaded. But, I want date field to be remembered from last item added to database (value that was in form before Submit button clicked).
here is my Controller, what I tried:
@GetMapping("/addNew")
public String addNewShow(@RequestParam(name = "date", required = false)
@DateTimeFormat(iso = ISO.DATE) Date date, Model model) {
Item item = new Item();
model.addAttribute("item", item);
item.setDate(date);
return "/poetracker/addNew";
}
@PostMapping("/addNew")
public String addNew(Item item) {
itemService.addNew(item);
return "redirect:/poetracker/addNew";
}
I tested passing date through query it works, but I want to be passed just with button click. Is there recommended way to do this? Java or JQuery, doesn't matter, just curious how it is done.
UPDATE: I am looking for this kind of functionality: In .net razor-pages I have properties
[BindProperty(Name = "month", SupportsGet = true)]
public int? SelectedMonth { get; set; }
[BindProperty(Name = "year", SupportsGet = true)]
public int? SelectedYear { get; set; }
[BindProperty(Name = "day", SupportsGet = true)]
public int? SelectedDay { get; set; }
and can redirect in Post like this:
public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
await repository.CreateAsync(Item!);
}
return RedirectToPage("Add", new
{
day = Item!.Day, month = Item!.Month, year = Item!.Year
});
}
Is there something like that in Java? Probably there is, I just need pointers how.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
