Advertisement
Viky__9

View problems

Oct 5th, 2021
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html xmlns:th="https://www.thymeleaf.org">
  3. <head>
  4. <title>Inserisci un nuovo libro</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. </head>
  7. <body>
  8. <div th:if="${libroForm}">
  9. <h1>Form</h1>
  10. <form action="#" th:action="@{/prova/libro}" th:object="${libroForm}" method="post">
  11. <p>Titolo: <input type="text" th:field="*{titolo}" /></p>
  12. <p>Autore: <input type="text" th:field="*{autore}" /></p>
  13. <p>Data Pubblicazioine: <input type="text" th:field="*{dataPubblicazione}" placeholder="yyyy-MM-dd" /> </p>
  14. <p>Numero Pezzi: <input type="number" th:field="*{numeroPezzi}" /></p>
  15. <p><input type="submit" value="Submit" a href="/libreriaProject/login/log"/> <input type="reset" value="Reset" /></p>
  16. </form>
  17. </div>
  18. <div th:unless="${book}">
  19. <h1>Tutti i libri</h1>
  20. <table>
  21. <thead>
  22. <tr>
  23. <th> Titolo </th>
  24. <th> Autore </th>
  25. <th> Numero Pezzi</th>
  26. <th>Data di Pubblicazione</th>
  27. </tr>
  28. </thead>
  29. <tbody>
  30.  
  31. <tr th:each="book : ${books}">
  32. <td><span th:text="${book.titolo}"> Title </span></td>
  33. <td><span th:text="${book.autore}"> Author </span></td>
  34. <td><span th:text="${book.numeroPezzi}"> Numero Pezzi</span></td>
  35. <td><span th:text="${book.dataPubblicazione}">Data pubblicazione</span></td>
  36. </tr>
  37. </tbody>
  38. </table>
  39. </div>
  40. </body>
  41. </html>
  42.  
  43.  
  44. -----------------end html + thymeleaf----------------
  45. ---------------------------------------START CONTROLLER---------------------------
  46.  
  47. import org.apache.log4j.Logger;
  48. import org.springframework.beans.factory.annotation.Autowired;
  49. import org.springframework.stereotype.Controller;
  50. import org.springframework.ui.Model;
  51. import org.springframework.web.bind.annotation.GetMapping;
  52. import org.springframework.web.bind.annotation.ModelAttribute;
  53. import org.springframework.web.bind.annotation.PostMapping;
  54. import org.springframework.web.bind.annotation.RequestMapping;
  55. import org.springframework.web.servlet.ModelAndView;
  56.  
  57. import teoresiGroup.web.model.LibriModel;
  58. import teoresiGroup.web.service.Interfacce.LibroService;
  59.  
  60. @Controller
  61. @RequestMapping("/prova")
  62. public class provaController {
  63. private static final Logger log= Logger.getLogger(provaController.class.getName());
  64.  
  65. @Autowired
  66. public LibroService libroService;
  67.  
  68.  
  69. @GetMapping("/lb")
  70. public ModelAndView hello(Model model)
  71. {
  72. log.info("Sono in libri form");
  73. LibriModel libri= new LibriModel();
  74. log.info("Dati inseriti: " + libri);
  75. model.addAttribute("libroForm",libri);
  76. return new ModelAndView("prova", "libroForm", new LibriModel());
  77.  
  78. }
  79. @PostMapping("/libro")
  80. public ModelAndView ciao(@ModelAttribute("libroForm") LibriModel libro) {
  81.  
  82. log.info("Sono in Libri aggiungi post");
  83.  
  84.  
  85. if(libro!=null)
  86. {
  87. log.info("i dati che sono arrivati: " + libro.getAutore() + " " + libro.getTitolo()+
  88. " " + libro.getNumeroPezzi());
  89.  
  90. libroService.add(libro);
  91.  
  92. return new ModelAndView("prova", "libroForm", libro);
  93. /*reindirizza alla pagina di registrazione. cambiare vesro visualizza tutto*/
  94. }
  95. else return new ModelAndView("error");
  96.  
  97. }
  98. @GetMapping("/all")
  99. public ModelAndView all(Model model) {
  100. Iterable<LibriModel> lib= libroService.getAll();
  101. lib.forEach((LibriModel l)->{
  102. model.addAttribute("books", lib);
  103. });
  104.  
  105.  
  106.  
  107. return new ModelAndView("prova");
  108.  
  109. }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement