Add BotonDTO class and update home page to use button fragments for dynamic button rendering
This commit is contained in:
parent
37afb26f59
commit
b56442d48b
|
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
package com.ieslamar.GestionInventario;
|
package com.ieslamar.GestionInventario;
|
||||||
|
import com.ieslamar.GestionInventario.dto.BotonDTO;
|
||||||
import com.ieslamar.GestionInventario.entities.Role;
|
import com.ieslamar.GestionInventario.entities.Role;
|
||||||
import com.ieslamar.GestionInventario.services.UserService;
|
import com.ieslamar.GestionInventario.services.UserService;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
|
@ -16,6 +17,11 @@ public class DataInitializer {
|
||||||
// userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com",null);
|
// userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com",null);
|
||||||
// };
|
// };
|
||||||
// }
|
// }
|
||||||
|
// public static void test(){
|
||||||
|
// BotonDTO boton1 = new BotonDTO("Inventario", "/Inventario/management");
|
||||||
|
// boton1.isRequireAdmin();
|
||||||
|
//
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.ieslamar.GestionInventario.controllers;
|
package com.ieslamar.GestionInventario.controllers;
|
||||||
|
|
||||||
|
|
||||||
|
import com.ieslamar.GestionInventario.dto.BotonDTO;
|
||||||
import com.ieslamar.GestionInventario.entities.User;
|
import com.ieslamar.GestionInventario.entities.User;
|
||||||
import com.ieslamar.GestionInventario.services.UserService;
|
import com.ieslamar.GestionInventario.services.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
@ -28,6 +29,11 @@ public class UserController {
|
||||||
|
|
||||||
@GetMapping("/home")
|
@GetMapping("/home")
|
||||||
public String homePage(Model model) {
|
public String homePage(Model model) {
|
||||||
|
List<BotonDTO> botones = List.of(
|
||||||
|
new BotonDTO("Inventario", "/Inventario/management", false, null),
|
||||||
|
new BotonDTO("Gestión", "/Inventario/management", true, "button_salir")
|
||||||
|
);
|
||||||
|
model.addAttribute("buttons", botones);
|
||||||
return "home"; // Retorna la vista home.html
|
return "home"; // Retorna la vista home.html
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,74 @@
|
||||||
|
package com.ieslamar.GestionInventario.dto;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
public class BotonDTO {
|
||||||
|
private String texto;
|
||||||
|
private String url;
|
||||||
|
private boolean requireAdmin;
|
||||||
|
private String cssClass;
|
||||||
|
|
||||||
|
public BotonDTO(String texto, String url) {
|
||||||
|
this(texto, url, false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BotonDTO(String texto, String url, boolean requireAdmin) {
|
||||||
|
this(texto, url, requireAdmin, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BotonDTO(String texto, String url, String cssClass) {
|
||||||
|
this(texto, url, false, cssClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BotonDTO(String texto, String url, boolean requireAdmin, String cssClass) {
|
||||||
|
this.texto = texto;
|
||||||
|
this.url = url;
|
||||||
|
this.requireAdmin = requireAdmin;
|
||||||
|
this.cssClass = cssClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTexto() {
|
||||||
|
return texto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTexto(String texto) {
|
||||||
|
this.texto = texto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRequireAdmin() {
|
||||||
|
return requireAdmin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRequireAdmin(boolean requireAdmin) {
|
||||||
|
this.requireAdmin = requireAdmin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCssClass() {
|
||||||
|
return cssClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCssClass(String cssClass) {
|
||||||
|
this.cssClass = cssClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "BotonDTO{" +
|
||||||
|
"texto='" + texto + '\'' +
|
||||||
|
", url='" + url + '\'' +
|
||||||
|
", requireAdmin=" + requireAdmin +
|
||||||
|
", cssClass='" + cssClass + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Binary file not shown.
|
|
@ -0,0 +1,28 @@
|
||||||
|
<!-- fragments/buttons.html -->
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.w3.org/1999/xhtml">
|
||||||
|
<body>
|
||||||
|
<div th:fragment="buttonList(buttons)">
|
||||||
|
<div class="container1">
|
||||||
|
<div th:each="button : ${buttons}">
|
||||||
|
<div th:utext="${button.isRequireAdmin()}"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<!-- <div th:if="${!button.isRequireAdmin()}">-->
|
||||||
|
<!-- <a th:href="@{${button.getUrl()}}">-->
|
||||||
|
<!-- <button th:class="${button.getCssClass()}" th:text="${button.getTexto()}"></button>-->
|
||||||
|
<!-- </a>-->
|
||||||
|
<!-- </div>-->
|
||||||
|
<!-- <div th:if="${button.isRequireAdmin()}">-->
|
||||||
|
<!-- <div sec:authorize="hasRole('ADMIN')">-->
|
||||||
|
<!-- <a th:href="@{${button.getUrl()}}">-->
|
||||||
|
<!-- <button th:class="${button.getCssClass()}" th:text="${button.getTexto()}"></button>-->
|
||||||
|
<!-- </a>-->
|
||||||
|
<!-- </div>-->
|
||||||
|
<!-- </div>-->
|
||||||
|
|
@ -1,27 +1,22 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="es" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.w3.org/1999/xhtml">
|
<html lang="es" xmlns:th="http://www.thymeleaf.org"
|
||||||
|
xmlns:sec="http://www.w3.org/1999/xhtml">
|
||||||
<head>
|
<head>
|
||||||
<title>Home</title>
|
<title>Home</title>
|
||||||
<link rel="stylesheet" th:href="@{/css/style.css}">
|
<link rel="stylesheet" th:href="@{/css/style.css}">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h2>Bienvenido a Gestión de Inventario</h2>
|
<h2>Bienvenido a Gestión de Inventario</h2>
|
||||||
<div class="container1">
|
<div th:each="button : ${buttons}">
|
||||||
<a href="/Inventario/management">
|
<div th:utext="${button.isRequireAdmin()}"></div>
|
||||||
<button>Inventario</button>
|
|
||||||
</a>
|
|
||||||
<!-- Botón visible solo para Administradores -->
|
|
||||||
<div sec:authorize="hasRole('ADMIN')">
|
|
||||||
<a href="/Inventario/management">
|
|
||||||
<button>Gestión</button>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
<!-- Formulario de Logout (POST) -->
|
|
||||||
<form th:action="@{/logout}" method="post">
|
|
||||||
<button class="button_salir" type="submit">Cerrar Sesión</button>
|
|
||||||
<!-- Token CSRF (obligatorio) -->
|
|
||||||
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
|
|
||||||
</form>
|
|
||||||
</div>
|
</div>
|
||||||
|
<!-- Reemplaza los botones por el fragmento reutilizable -->
|
||||||
|
<div th:replace="fragments/buttons :: buttonList(buttons)"></div>
|
||||||
|
|
||||||
|
<!-- Formulario de Logout (estático y fuera del fragmento) -->
|
||||||
|
<form th:action="@{/logout}" method="post">
|
||||||
|
<button class="button_salir" type="submit">Cerrar Sesión</button>
|
||||||
|
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
|
||||||
|
</form>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue