Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Controller
- @RequestMapping(value = "/admin")
- public class ControllerAdminPompa {
- @Autowired
- PompaServiceImpl pompaService;
- /* Gestione Pompe */
- @GetMapping("/pompe")
- public String pompeList(Model model) {
- Iterable<Pompa> allPompe = pompaService.findAll();
- model.addAttribute("pompe", allPompe);
- return "/admin/visualizzaPompe";
- }
- @GetMapping("/modificaPompa/{idPompa}")
- public String updatePath(@PathVariable("idPompa") int idPompa, Model model) {
- Optional<Pompa> p = pompaService.findById(idPompa);
- if (p.isPresent()) {
- model.addAttribute("pompa", p.get());
- System.out.println("get: " + p.toString());
- return "/admin/modificaPompa";
- } else {
- System.out.println("La pompa non esiste");
- return "redirect:/admin/pompe";
- }
- }
- @PostMapping(value = "/modificaPompa/{idPompa}")
- public String updatePath(@ModelAttribute("pompa") Pompa editedPompa, @PathVariable("idPompa") int idPompa,
- BindingResult result, RedirectAttributes redirectAttributes, Model model) {
- System.out.println("Post: " + editedPompa.toString() + idPompa);
- redirectAttributes.addFlashAttribute("message", "Impossibile Effettuare la modifica");
- redirectAttributes.addFlashAttribute("alertClass", "alert alert-danger");
- if (result.hasErrors()) {
- System.out.println("BindingResult : C'è stato un errore");
- }
- if (result.getSuppressedFields().length > 0) {
- System.out.println("ValidationBindingResult : "
- + StringUtils.arrayToCommaDelimitedString(result.getSuppressedFields()));
- } else {
- Optional<Pompa> p = pompaService.findById(idPompa);
- // if (editedPompa.getId()==null) {//significa che è una modifica
- if (p.isPresent()) {
- editedPompa.setQRcode(p.get().getQRcode());
- editedPompa.setId(idPompa);
- pompaService.save(editedPompa);
- redirectAttributes.addFlashAttribute("message", "Modifica Avvenuta Con Successo");
- redirectAttributes.addFlashAttribute("alertClass", "alert alert-success");
- return "redirect:/admin/modificaPompa/" + editedPompa.getId();
- } else {
- System.out.println("pompa inesistente");
- }
- }
- return "redirect:/admin/pompe";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement