Zum Inhalt springen

springboot.firststeps(lib)

libroControlador.java

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page import="es.uma.taw.libroswebapp.entity.LibroEntity" %>
<%@ page import="java.util.List" %>
<%@ page import="es.uma.taw.libroswebapp.entity.GeneroEntity" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Catálogo de libros disponibles</title>
</head>

<%
    List<LibroEntity> lista = (List<LibroEntity>) request.getAttribute("libros");
%>
<body>

<jsp:include page="cabecera.jsp"/>

<h1>Catálogo de libros disponibles</h1>

<form:form modelAttribute="filtro" method="post" action="/libros/filtrar">
    Contiene:<form:input path="cadena"/> y pertenece a los siguientes generos:<br>
    <form:checkboxes path="generosList" items="${generos}" itemLabel="generoname"/> <br>
    <form:button>Filtrar</form:button>
</form:form>

<table border="1">
    <tr>
        <th>ID</th>
        <th>NOMBRE</th>
        <th>AÑO PUBLICACIÓN</th>
        <th>EDITORIAL</th>
        <th>Generos</th>
        <th></th>
        <th></th>
    </tr>
<%
    for (LibroEntity libro: lista) {
%>
    <tr>
        <td><%= libro.getLibroid() %></td>
        <td><%= libro.getLibroname() %></td>
        <td><%= libro.getYearreleased() %></td>
        <td><%= libro.getEditorialid().getEditorialname() %></td>

        <td>
            <%
                List<GeneroEntity> generos = libro.getGeneroList();
                for (int i = 0; i < generos.size(); i++) {
                    out.print(generos.get(i).getGeneroname());
                    if (i < generos.size() - 1) out.print(", ");
                }
            %>
        </td>

        <td><form method="post" action="/libros/editar">
            <input type="hidden" name="id" value="<%= libro.getLibroid() %>">
            <input type="submit" value="Editar"/>
        </form></td>
        <td><a href="/libros/borrar?id=<%= libro.getLibroid() %>"  onclick="return confirm('¿Está seguro de que quiere borrar el libro <%=libro.getLibroname() %>?')">Borrar</a> </td>
    </tr>
<%
    }
%>
</table>
<form method="post" action="/libros/editar">
    <input type="submit" value="Nuevo libro">
</form>
</body>
</html>

loginController.java

@Controller
public class LoginController {

    @Autowired
    UsuarioRepository usuarioRepository;

    @GetMapping("/")
    public String doInit(Model model){
        model.addAttribute("modelUsuario", new UsuarioEntity());
        return "login";
    }

    @PostMapping("/autentica")
    public String doAutentica(@ModelAttribute("modelUsuario")UsuarioEntity usuario, Model model, HttpSession session){
        UsuarioEntity usuarioAut = usuarioRepository.findByApellidoYContrasenya(usuario.getUsuariosurname(), usuario.getUsuariopassword());
        if(usuarioAut==null){
            model.addAttribute("error", "Error de autentificacion");
            return "login";
        }else{
            session.setAttribute("user", usuarioAut);
            return "redirect:/libros/";
        }
    }

    @GetMapping("/salir")
    public String doSalir(HttpSession session){
        //session.removeAttribute("user");
        session.invalidate();
        return "redirect:/";
    }
}

Vista

libros.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page import="es.uma.taw.libroswebapp.entity.LibroEntity" %>
<%@ page import="java.util.List" %>
<%@ page import="es.uma.taw.libroswebapp.entity.GeneroEntity" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Catálogo de libros disponibles</title>
</head>

<%
    List<LibroEntity> lista = (List<LibroEntity>) request.getAttribute("libros");
%>
<body>

<jsp:include page="cabecera.jsp"/>

<h1>Catálogo de libros disponibles</h1>

<form:form modelAttribute="filtro" method="post" action="/libros/filtrar">
    Contiene:<form:input path="cadena"/> y pertenece a los siguientes generos:<br>
    <form:checkboxes path="generosList" items="${generos}" itemLabel="generoname"/> <br>
    <form:button>Filtrar</form:button>
</form:form>

<table border="1">
    <tr>
        <th>ID</th>
        <th>NOMBRE</th>
        <th>AÑO PUBLICACIÓN</th>
        <th>EDITORIAL</th>
        <th>Generos</th>
        <th></th>
        <th></th>
    </tr>
<%
    for (LibroEntity libro: lista) {
%>
    <tr>
        <td><%= libro.getLibroid() %></td>
        <td><%= libro.getLibroname() %></td>
        <td><%= libro.getYearreleased() %></td>
        <td><%= libro.getEditorialid().getEditorialname() %></td>

        <td>
            <%
                List<GeneroEntity> generos = libro.getGeneroList();
                for (int i = 0; i < generos.size(); i++) {
                    out.print(generos.get(i).getGeneroname());
                    if (i < generos.size() - 1) out.print(", ");
                }
            %>
        </td>

        <td><form method="post" action="/libros/editar">
            <input type="hidden" name="id" value="<%= libro.getLibroid() %>">
            <input type="submit" value="Editar"/>
        </form></td>
        <td><a href="/libros/borrar?id=<%= libro.getLibroid() %>"  onclick="return confirm('¿Está seguro de que quiere borrar el libro <%=libro.getLibroname() %>?')">Borrar</a> </td>
    </tr>
<%
    }
%>
</table>
<form method="post" action="/libros/editar">
    <input type="submit" value="Nuevo libro">
</form>
</body>
</html>

libro.jsp

<%@ page import="es.uma.taw.libroswebapp.entity.LibroEntity" %>
<%@ page import="es.uma.taw.libroswebapp.entity.EditorialEntity" %>
<%@ page import="java.util.List" %>
<%@ page import="es.uma.taw.libroswebapp.entity.GeneroEntity" %>
<%@ page import="es.uma.taw.libroswebapp.entity.EscritorEntity" %><%--
  Created by IntelliJ IDEA.
  User: guzman
  Date: 26/3/25
  Time: 11:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>

<%
  boolean esEditar = true;
  LibroEntity libro = (LibroEntity)request.getAttribute("libro");
  if (libro.getLibroid() == null) esEditar = false;
  List<EditorialEntity> editoriales = (List<EditorialEntity>) request.getAttribute("editoriales");
  List<GeneroEntity> generos = (List<GeneroEntity>) request.getAttribute("generos");
  List<EscritorEntity> escritores = (List<EscritorEntity>)request.getAttribute("escritores");
%>

<head>
  <title><%= (esEditar? "Edición del" : "Nuevo") %> libro</title>
</head>
<body>
<h1><%= (esEditar? "Editar" : "Nuevo") %> libro</h1>

<form method="post" action="/libros/guardar">
  <input type="hidden" name="id" value="<%= (libro.getLibroid()==null? -1: libro.getLibroid()) %>">
  Nombre: <input type="text" name="nombre" value="<%= (libro.getLibroname()!=null?libro.getLibroname() :"") %>"> <br/>
  Año de publicación: <input type="text" name="anyo" value="<%= (libro.getYearreleased()==0? "" : libro.getYearreleased()) %>"> <br/>
  Editorial:
  <select name="editorial">
  <%
    for (EditorialEntity laeditorial: editoriales) {
      String seleccionado = "";
      if (esEditar && laeditorial.getEditorialid() == libro.getEditorialid().getEditorialid()) {
        seleccionado = "selected";
      }

  %>

      <option value="<%= laeditorial.getEditorialid() %>" <%=seleccionado%>  ><%= laeditorial.getEditorialname() %></option>
  <% } %>
  </select>
  <br/>
  Géneros:
  <%
    for (GeneroEntity genero: generos) {
      String seleccionado = "";
      if (esEditar && libro.getGeneroList().contains(genero)) {
        seleccionado = "checked";
      }
  %>
  <input type="checkbox" <%= seleccionado  %> name="generos" value="<%= genero.getGeneroid() %>"> <%= genero.getGeneroname() %>
  <%
    }
  %>
  <br/>
  Escritores:
  <select name="escritores" multiple>
  <%
    for (EscritorEntity escritor: escritores) {
      String seleccionado = "";
      if (esEditar && libro.getEscritorList().contains(escritor)) {
        seleccionado = "selected";
      }
  %>
  <option <%= seleccionado  %> value="<%= escritor.getEscritorid() %>"> <%= escritor.getEscritorname() %>
  <%
    }
  %>
  </select>
  <br/>
  <input type="submit" value="Guardar">
</form>


</body>
</html>

login.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
</head>
<%
    String error = (String) request.getAttribute("error");
%>
<body>
<h1>Login</h1>

<%
    if(error!=null){
%>
    <%=error%> <br><br>
<%
    }
%>

<form:form action="/autentica" method="post" modelAttribute="modelUsuario">
    Usuario: <form:input path="usuariosurname"/> <br>
    Contraseña: <form:password path="usuariopassword"/> <br>
    <form:button>Enviar</form:button>
</form:form>

</body>
</html>

cabecera.jsp

<%@ page import="es.uma.taw.libroswebapp.entity.UsuarioEntity" %>
<%@ page import="es.uma.taw.libroswebapp.entity.LibroEntity" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>Cabecera</title>
</head>

<%
    UsuarioEntity user = (UsuarioEntity) session.getAttribute("user");
    List<LibroEntity> lista = (List<LibroEntity>) request.getAttribute("libros");
%>

<body>
<table width="100%">
    <tr>
        <td> <a href="/libros">Libros</a> </td>
        <td> <%=lista.get(0).getLibroname()%> </td>
        <td>Editoriales</td>
        <td>Bienvenido, <%=user.getUsuariosurname()%> ( <a href="/salir">Salir</a> ) </td>
    </tr>
</table>


</body>
</html>

ui(Filtro)

package es.uma.taw.libroswebapp.ui;

import es.uma.taw.libroswebapp.entity.GeneroEntity;
import lombok.Data;

import java.util.List;

@Data
public class Filtro {
    String cadena;
    List<GeneroEntity> generosList;

}

Repos

public interface LibroRepository extends JpaRepository<LibroEntity, Integer> {

    @Query("select l from LibroEntity l where l.libroname like concat('%',:cadena,'%')")
    public List<LibroEntity> findByCadena(@Param("cadena")String cadena);

    @Query("select l from LibroEntity l where l.usuarioid.usuarioid = :id ")
    public List<LibroEntity> findByUsuarioId(@Param("id")Integer id);


    @Query("select l from LibroEntity l join l.generoList g where (l.libroname like concat('%',:cadena,'%')) and (g in :generos)")
    public List<LibroEntity> findByCadenaYGeneros(@Param("cadena")String cadena, @Param("generos")List<GeneroEntity> generos);
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert