Data is not loaded into Thymeleaf template

Viewed 19

I have a problem with my Spring app. When I send form data in post request and use return (template name) in my controller, data is not being loaded and I just get pure thymeleaf template and I have to refresh page to get my data. But when I use return redirect:/deals" everything works fine. Can you explain me what am I doing wrong and and what is the difference? Have the same situation in every other method (put, delete etc.)

@Controller
@RequestMapping("/deals")

public class DealController {

private final DealService dealService;

public DealController(DealService dealService) {
    this.dealService = dealService;
}

@GetMapping
public String showDeals(Model model, @RequestParam Optional<String> surname){

    List<Deal> list = dealService.getDeals(surname);
    double totalProfit = dealService.calculateTotalProfit(list);

    model.addAttribute("deal", new Deal());
    model.addAttribute("deals", list);
    model.addAttribute("sum", totalProfit);
    model.addAttribute("num", list.size());
    return "deals";
}

@PostMapping
public String saveDeal(@ModelAttribute("deal") @Valid Deal deal,
                       BindingResult result,
                       Model model) {
    if(result.hasErrors()){
        Deal emptyDeal = new Deal();
        BeanUtils.copyProperties(deal, emptyDeal);
        return "deals";
    }
    dealService.saveDeal(deal);
    model.addAttribute("deal", new Deal());
    return "redirect:/deals";
}
0 Answers
Related