apuntes:spring_web
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| apuntes:spring_web [2021/11/02 17:57] – [Fragmentos] Santiago Faci | apuntes:spring_web [2021/11/21 23:43] (current) – [Relaciones entre clases] Santiago Faci | ||
|---|---|---|---|
| Line 256: | Line 256: | ||
| * Utilizando el jar que podemos generar con el comando '' | * Utilizando el jar que podemos generar con el comando '' | ||
| + | ===== Modelo de datos ===== | ||
| + | |||
| + | <code java> | ||
| + | @Data | ||
| + | @AllArgsConstructor | ||
| + | @NoArgsConstructor | ||
| + | @Entity(name = " | ||
| + | public class Product { | ||
| + | |||
| + | @Id | ||
| + | @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| + | private long id; | ||
| + | @Column | ||
| + | private String name; | ||
| + | @Column | ||
| + | private String description; | ||
| + | @Column | ||
| + | private String category; | ||
| + | @Column | ||
| + | private float price; | ||
| + | @Column | ||
| + | private LocalDateTime creationDate; | ||
| + | @Column | ||
| + | private String observations; | ||
| + | @Column | ||
| + | private int quantity; | ||
| + | } | ||
| + | </ | ||
| + | ==== Relaciones entre clases ==== | ||
| + | |||
| + | <code java> | ||
| + | @Data | ||
| + | @AllArgsConstructor | ||
| + | @NoArgsConstructor | ||
| + | @Entity(name = " | ||
| + | public class Product { | ||
| + | |||
| + | @Id | ||
| + | @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| + | private long id; | ||
| + | @Column | ||
| + | private String name; | ||
| + | @Column | ||
| + | private String description; | ||
| + | @Column | ||
| + | private String category; | ||
| + | @Column | ||
| + | private float price; | ||
| + | @Column | ||
| + | private LocalDateTime creationDate; | ||
| + | @Column | ||
| + | private String observations; | ||
| + | @Column | ||
| + | private int quantity; | ||
| + | | ||
| + | @ManyToOne | ||
| + | @JoinColumn(name = " | ||
| + | private User user; | ||
| + | @ManyToOne | ||
| + | @JoinColumn(name = " | ||
| + | private Category category; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code java> | ||
| + | @Data | ||
| + | @AllArgsConstructor | ||
| + | @NoArgsConstructor | ||
| + | @Entity(name = " | ||
| + | public class User { | ||
| + | |||
| + | @Id | ||
| + | @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| + | private long id; | ||
| + | @Column | ||
| + | private String dni; | ||
| + | @Column | ||
| + | private String name; | ||
| + | @Column | ||
| + | private String surname; | ||
| + | @Column(name = " | ||
| + | private LocalDate birthDate; | ||
| + | |||
| + | @OneToMany(mappedBy = " | ||
| + | private List< | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code java> | ||
| + | @Data | ||
| + | @AllArgsConstructor | ||
| + | @NoArgsConstructor | ||
| + | @Entity(name = " | ||
| + | public class Category { | ||
| + | |||
| + | @Id | ||
| + | @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| + | private long id; | ||
| + | @Column | ||
| + | private String name; | ||
| + | @Column | ||
| + | private String description; | ||
| + | @Column | ||
| + | private float discount; | ||
| + | |||
| + | @OneToMany(mappedBy = " | ||
| + | private List< | ||
| + | } | ||
| + | </ | ||
| + | ===== Gestión de errores ===== | ||
| + | |||
| + | <code java> | ||
| + | @RequestMapping(value = "/ | ||
| + | public String product(Model model, @PathVariable long id) throws ProductNotFoundException { | ||
| + | Product product = productService.findProduct(id); | ||
| + | model.addAttribute(" | ||
| + | return " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code java> | ||
| + | public class ProductNotFoundException extends Exception { | ||
| + | |||
| + | public ProductNotFoundException(String message) { | ||
| + | super(message); | ||
| + | } | ||
| + | |||
| + | public ProductNotFoundException() { | ||
| + | super(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code java> | ||
| + | @ExceptionHandler(ProductNotFoundException.class) | ||
| + | public String handleException(HttpServletRequest request, ProductNotFoundException exception) { | ||
| + | return " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <file html product_error.html> | ||
| + | < | ||
| + | <html lang=" | ||
| + | < | ||
| + | <meta charset=" | ||
| + | < | ||
| + | </ | ||
| + | < | ||
| + | <p>El producto que intentas localizar no está disponible</ | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <code java> | ||
| + | @ExceptionHandler | ||
| + | public String handleException(HttpServletRequest request, Exception exception) { | ||
| + | return " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <file html error.html> | ||
| + | < | ||
| + | <html lang=" | ||
| + | < | ||
| + | <meta charset=" | ||
| + | < | ||
| + | </ | ||
| + | < | ||
| + | <p>La página que has solicitado no está disponible</ | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| ===== Plantillas. Thymeleaf ===== | ===== Plantillas. Thymeleaf ===== | ||
| Line 261: | Line 435: | ||
| ==== Fragmentos ==== | ==== Fragmentos ==== | ||
| + | |||
| + | <file html fragments/ | ||
| + | <head xmlns: | ||
| + | <meta charset=" | ||
| + | <meta name=" | ||
| + | <title th: | ||
| + | |||
| + | <link href="/ | ||
| + | </ | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <code html> | ||
| + | < | ||
| + | <html lang=" | ||
| + | <head th: | ||
| + | < | ||
| + | . . . | ||
| + | . . . | ||
| + | </ | ||
| <file html fragments/ | <file html fragments/ | ||
| Line 273: | Line 467: | ||
| <code html> | <code html> | ||
| + | . . . | ||
| + | . . . | ||
| <footer th: | <footer th: | ||
| + | . . . | ||
| + | . . . | ||
| </ | </ | ||
| + | ==== Integración con Bootstrap ==== | ||
| - | <code html> | + | ==== Formularios ==== |
| - | < | + | |
| - | <html lang=" | + | |
| - | <head th:replace=" | + | |
| - | < | + | |
| - | <header th:replace=" | + | |
| - | < | + | |
| - | <section th:replace=" | + | |
| - | </ | + | |
| - | ==== Integración con Bootstrap ==== | ||
| - | ==== Procesar formularios ==== | + | === Formulario de registro |
| - | ===== Contenido estático | + | |
| + | === Formulario de búsqueda === | ||
| + | |||
| + | ==== Contenido estático ==== | ||
| <code html> | <code html> | ||
| Line 299: | Line 492: | ||
| <img th: | <img th: | ||
| </ | </ | ||
| + | |||
| + | ===== Seguridad ===== | ||
| + | |||
| + | |||
| + | ==== Inicio de sesión ===== | ||
| ---- | ---- | ||
apuntes/spring_web.1635875837.txt.gz · Last modified: 2021/11/02 17:57 by Santiago Faci