Commit d79bd36b by Patryk Czarnik

redirect po poprawnym zapisaniu produktu

parent 8b0dd8ea
......@@ -105,15 +105,19 @@ public class ProductController {
// Metoda jest wywoływana zawsze, a to programista ma sprawdzić czy walidacja się powiodła.
// W BindingResult znajdują się też informacje o błędach.
if(bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
// model.addAttribute("errors", bindingResult.getAllErrors());
// błędów już nie dodajemy oddzielne, bo będą wyświeltlone przez f:error
// ponownie wyświetlamy formularz
return "product_form";
} else try {
// Gdy próbujemy wywołać save, a obiekt nie spełnia wymagań validation, to wtedy Hibernate zablokuje taki zapis (wyrzuci wyjątek).
productRepository.save(product);
model.addAttribute("saved", true);
// Po pomyślnym zapisaniu przechodzimy na stronę tego produktu
return "redirect:/products/" + product.getProductId();
} catch(Exception e) {
model.addAttribute("errors", ExceptionUtils.allMessages(e));
}
return "product_form";
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment