From e88779db2d72a59555b72d295e52723c76de9263 Mon Sep 17 00:00:00 2001 From: Javier Ferreira Date: Mon, 8 Nov 2021 15:37:01 -0300 Subject: [PATCH] form para agregar y editar tecnologia --- curriculumsearch/src/main/java/com/roshka/controller/TecnologiaController.java | 32 +++++++++++++++++++++++++------- curriculumsearch/src/main/java/com/roshka/repositorio/TecnologiaRepository.java | 4 +++- curriculumsearch/src/main/webapp/jsp/tecnologia-form.jsp | 2 +- curriculumsearch/src/main/webapp/jsp/tecnologias.jsp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 curriculumsearch/src/main/webapp/jsp/tecnologias.jsp diff --git a/curriculumsearch/src/main/java/com/roshka/controller/TecnologiaController.java b/curriculumsearch/src/main/java/com/roshka/controller/TecnologiaController.java index 11dc881..b73ee33 100644 --- a/curriculumsearch/src/main/java/com/roshka/controller/TecnologiaController.java +++ b/curriculumsearch/src/main/java/com/roshka/controller/TecnologiaController.java @@ -6,10 +6,13 @@ import com.roshka.repositorio.TecnologiaRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; 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.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; @Controller public class TecnologiaController { @@ -23,15 +26,30 @@ public TecnologiaController(TecnologiaRepository tecRepo){ } -@GetMapping("/tecnologia") -public String addtecnologiaView(Model model) { - model.addAttribute("tecnologia", new Tecnologia()); +@GetMapping(path = {"/tecnologia","/tecnologia/{id}"}) +public String addtecnologiaView(Model model,@PathVariable(required = false) Long id) { + + + if(id == null) model.addAttribute("tecnologia", new Tecnologia()); + else model.addAttribute("tecnologia", tecRepo.getById(id)); 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:/"; } diff --git a/curriculumsearch/src/main/java/com/roshka/repositorio/TecnologiaRepository.java b/curriculumsearch/src/main/java/com/roshka/repositorio/TecnologiaRepository.java index 3bfc4ca..fed1587 100644 --- a/curriculumsearch/src/main/java/com/roshka/repositorio/TecnologiaRepository.java +++ b/curriculumsearch/src/main/java/com/roshka/repositorio/TecnologiaRepository.java @@ -2,9 +2,11 @@ package com.roshka.repositorio; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + import com.roshka.modelo.Tecnologia; public interface TecnologiaRepository extends JpaRepository { - + public List findByNombreContainingIgnoreCase(String nombre); } diff --git a/curriculumsearch/src/main/webapp/jsp/tecnologia-form.jsp b/curriculumsearch/src/main/webapp/jsp/tecnologia-form.jsp index cb244c0..caceeea 100644 --- a/curriculumsearch/src/main/webapp/jsp/tecnologia-form.jsp +++ b/curriculumsearch/src/main/webapp/jsp/tecnologia-form.jsp @@ -23,7 +23,7 @@ - + name: diff --git a/curriculumsearch/src/main/webapp/jsp/tecnologias.jsp b/curriculumsearch/src/main/webapp/jsp/tecnologias.jsp new file mode 100644 index 0000000..975ce1b --- /dev/null +++ b/curriculumsearch/src/main/webapp/jsp/tecnologias.jsp @@ -0,0 +1,49 @@ +<%@ 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" %> + + + + + + + Tecnologia + + + + +
+
+ + + +
+ Agregar Nueva Tecnologia +
+
+ + + + + + + + + + + + + + + + + + + + + +
#Tecnologia
${sta.index+1}${tecnologia.getNombre()}Editar tecnologia
+
+ + + \ No newline at end of file -- libgit2 0.26.0