Commit e88779db by Javier Ferreira

form para agregar y editar tecnologia

parent b9ac88e2
...@@ -6,10 +6,13 @@ import com.roshka.repositorio.TecnologiaRepository; ...@@ -6,10 +6,13 @@ import com.roshka.repositorio.TecnologiaRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller @Controller
public class TecnologiaController { public class TecnologiaController {
...@@ -23,15 +26,30 @@ public TecnologiaController(TecnologiaRepository tecRepo){ ...@@ -23,15 +26,30 @@ public TecnologiaController(TecnologiaRepository tecRepo){
} }
@GetMapping("/tecnologia") @GetMapping(path = {"/tecnologia","/tecnologia/{id}"})
public String addtecnologiaView(Model model) { public String addtecnologiaView(Model model,@PathVariable(required = false) Long id) {
model.addAttribute("tecnologia", new Tecnologia());
if(id == null) model.addAttribute("tecnologia", new Tecnologia());
else model.addAttribute("tecnologia", tecRepo.getById(id));
return "tecnologia-form"; return "tecnologia-form";
} }
@PostMapping("/tecnologia")
public String addtecnologia(@ModelAttribute("tecnologia") Tecnologia tecnologia) {
tecRepo.save(tecnologia);
@RequestMapping("/tecnologias")
public String menuTecnologias(Model model,
@RequestParam(required = false) String nombre
) {
if(nombre == null || nombre.trim().isEmpty()) model.addAttribute("tecnologias", tecRepo.findAll());
else model.addAttribute("tecnologias", tecRepo.findByNombreContainingIgnoreCase(nombre));
return "tecnologias";
}
@PostMapping(path = {"/tecnologia","/tecnologia/{id}"})
public String addtecnologia(@ModelAttribute Tecnologia tecnologia, BindingResult result, @PathVariable(required = false) Long id) {
if(result.hasErrors());
if(id != null ) tecnologia.setId(id);
tecRepo.save(tecnologia);
System.out.println(tecnologia.getNombre());
return "redirect:/"; return "redirect:/";
} }
......
...@@ -2,9 +2,11 @@ package com.roshka.repositorio; ...@@ -2,9 +2,11 @@ package com.roshka.repositorio;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import com.roshka.modelo.Tecnologia; import com.roshka.modelo.Tecnologia;
public interface TecnologiaRepository extends JpaRepository<Tecnologia,Long> { public interface TecnologiaRepository extends JpaRepository<Tecnologia,Long> {
public List<Tecnologia> findByNombreContainingIgnoreCase(String nombre);
} }
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
</head> </head>
<body> <body>
<form:form action="/tecnologia" method="post" modelAttribute="tecnologia"> <form:form action="/tecnologia/${tecnologia.id == null ? '' : tecnologia.id}" method="post" modelAttribute="tecnologia">
<form:label path="nombre">name: </form:label> <form:input type="text" path="nombre"/> <form:label path="nombre">name: </form:label> <form:input type="text" path="nombre"/>
<input type="submit" value="submit"/> <input type="submit" value="submit"/>
</form:form> </form:form>
......
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tecnologia</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</head>
<body class="container">
<div>
<form>
<label for="tecnologias">Nombre:</label>
<input type="text" name="nombre" id="nombre" value="${param.nombre}"/>
<input type="submit" value="Buscar">
</form>
<a href="/tecnologia">Agregar Nueva Tecnologia</a>
</div>
<div>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Tecnologia</th>
</tr>
</thead>
<tbody>
<c:forEach items="${tecnologias}" var="tecnologia" varStatus="sta">
<tr>
<th scope="row">${sta.index+1}</th>
<td>${tecnologia.getNombre()}</td>
<td><a href="/tecnologia/${tecnologia.id}">Editar tecnologia</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment