User Tools

Site Tools


apuntes:spring_web

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
apuntes:spring_web [2021/11/02 17:56] – [Fragmentos] Santiago Faciapuntes: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 ''mvn package'' y ejecutarlo con el comando ''java -jar''. El ''.jar'' generado lo podremos encontrar en la carpeta ''target''   * Utilizando el jar que podemos generar con el comando ''mvn package'' y ejecutarlo con el comando ''java -jar''. El ''.jar'' generado lo podremos encontrar en la carpeta ''target''
  
 +===== Modelo de datos =====
 +
 +<code java>
 +@Data
 +@AllArgsConstructor
 +@NoArgsConstructor
 +@Entity(name = "products")
 +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;
 +}
 +</code>
 +==== Relaciones entre clases ====
 +
 +<code java>
 +@Data
 +@AllArgsConstructor
 +@NoArgsConstructor
 +@Entity(name = "products")
 +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 = "user_id")
 +    private User user;
 +    @ManyToOne
 +    @JoinColumn(name = "category_id")
 +    private Category category;
 +}
 +</code>
 +
 +<code java>
 +@Data
 +@AllArgsConstructor
 +@NoArgsConstructor
 +@Entity(name = "users")
 +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 = "birth_date")
 +    private LocalDate birthDate;
 +
 +    @OneToMany(mappedBy = "user")
 +    private List<Product> products;
 +}
 +</code>
 +
 +<code java>
 +@Data
 +@AllArgsConstructor
 +@NoArgsConstructor
 +@Entity(name = "categories")
 +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 = "category")
 +    private List<Product> products;
 +}
 +</code>
 +===== Gestión de errores =====
 +
 +<code java>
 +@RequestMapping(value = "/product/{id}")
 +public String product(Model model, @PathVariable long id) throws ProductNotFoundException {
 +    Product product = productService.findProduct(id);
 +    model.addAttribute("product", product);
 +    return "product";
 +}
 +</code>
 +
 +<code java>
 +public class ProductNotFoundException extends Exception {
 +
 +    public ProductNotFoundException(String message) {
 +        super(message);
 +    }
 +
 +    public ProductNotFoundException() {
 +        super();
 +    }
 +}
 +</code>
 +
 +<code java>
 +@ExceptionHandler(ProductNotFoundException.class)
 +public String handleException(HttpServletRequest request, ProductNotFoundException exception) {
 +    return "product_error";
 +}
 +</code>
 +
 +<file html product_error.html>
 +<!DOCTYPE html>
 +<html lang="en">
 +<head>
 +    <meta charset="UTF-8">
 +    <title>Amazon</title>
 +</head>
 +<body>
 +<p>El producto que intentas localizar no está disponible</p>
 +<p>Prueba a buscarlo en nuestro <a href="/catalog">Catálogo</a></p>
 +</body>
 +</html>
 +</file>
 +
 +<code java>
 +@ExceptionHandler
 +public String handleException(HttpServletRequest request, Exception exception) {
 +    return "error";
 +}
 +</code>
 +
 +<file html error.html>
 +<!DOCTYPE html>
 +<html lang="en">
 +<head>
 +    <meta charset="UTF-8">
 +    <title>Amazon</title>
 +</head>
 +<body>
 +<p>La página que has solicitado no está disponible</p>
 +<p>Vuelva a intentarlo más tarde</p>
 +</body>
 +</html>
 +</file>
 ===== Plantillas. Thymeleaf ===== ===== Plantillas. Thymeleaf =====
  
Line 261: Line 435:
  
 ==== Fragmentos ==== ==== Fragmentos ====
 +
 +<file html fragments/head.html>
 +<head xmlns:th="http://www.w3.org/1999/xhtml">
 +    <meta charset="utf-8">
 +    <meta name="viewport" content="width=device-width, initial-scale=1">
 +    <title th:text="${title}"></title>
 +
 +    <link href="/webjars/bootstrap/5.0.0-beta2/css/bootstrap.min.css" rel="stylesheet">
 +</head>
 +<body>
 +</file>
 +
 +<code html>
 +<!doctype html>
 +<html lang="es" xmlns:th="http://www.w3.org/1999/xhtml">
 +<head th:replace="fragments/head :: head(title='Code & Coke')"></head>
 +<body>
 +. . .
 +. . .
 +</code>
  
 <file html fragments/footer.html> <file html fragments/footer.html>
Line 273: Line 467:
  
 <code html> <code html>
 +. . .
 +. . .
 <footer th:replace="fragments/footer"></footer> <footer th:replace="fragments/footer"></footer>
 +. . .
 +. . .
 </code> </code>
- 
 ==== Integración con Bootstrap ==== ==== Integración con Bootstrap ====
  
-==== Procesar formularios ====+==== Formularios ====
  
-===== Contenido estático =====+ 
 +=== Formulario de registro === 
 + 
 + 
 +=== Formulario de búsqueda === 
 + 
 +==== Contenido estático ====
  
 <code html> <code html>
Line 289: Line 492:
 <img th:src="@{'/images/' + ${product.image}}"> <img th:src="@{'/images/' + ${product.image}}">
 </code> </code>
 +
 +===== Seguridad =====
 +
 +
 +==== Inicio de sesión =====
  
 ---- ----
apuntes/spring_web.1635875805.txt.gz · Last modified: 2021/11/02 17:56 by Santiago Faci