Browse Source

Create - controlardor Guardar

master
Juanjo 1 year ago
parent
commit
15d132a8f3
2 changed files with 9 additions and 8 deletions
  1. +6
    -3
      src/main/java/es/fp/edu/conecta2/controladores/WebControlador.java
  2. +3
    -5
      src/main/resources/templates/nuevo_usuario.html

+ 6
- 3
src/main/java/es/fp/edu/conecta2/controladores/WebControlador.java View File

@ -7,9 +7,7 @@ import io.micrometer.common.util.StringUtils;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
@Controller @Controller
@ -30,6 +28,11 @@ public class WebControlador {
modelo.addAttribute("user",usuario); modelo.addAttribute("user",usuario);
return "nuevo_usuario"; return "nuevo_usuario";
} }
@PostMapping("/guardar")
public String guardarUser (@ModelAttribute("user") User usuario) {
servicioUser.registrar(usuario);
return "redirect:/";
}
@GetMapping ("/borrar") @GetMapping ("/borrar")
public String borrarUser(Model modelo){ public String borrarUser(Model modelo){


+ 3
- 5
src/main/resources/templates/nuevo_usuario.html View File

@ -16,7 +16,7 @@
<main> <main>
<div class="container"> <div class="container">
<form th:action="@{/}" th:object="${user}" method="post">
<form th:action="@{/guardar}" th:object="${user}" method="post">
<table class="table table-sm"> <table class="table table-sm">
<thead> <thead>
<tr> <tr>
@ -25,11 +25,9 @@
<th scope="col"></th> <th scope="col"></th>
</thead> </thead>
<tbody> <tbody>
<th><input type="text" name="idUsuario" th:field="${user.idUsuario}" class="form-control" placeholder="Nº de usuario" required></th>
<td><input type="text" name="nombres" th:field="${user.nombres}" class="form-control" placeholder="Nombre y apellidos" required></td>
<th><input type="text" name="idUsuario" th:field="*{idUsuario}" class="form-control" placeholder="Nº de usuario" required></th>
<td><input type="text" name="nombres" th:field="*{nombres}" class="form-control" placeholder="Nombre y apellidos" required></td>
<td><button class="btn btn-success" >CREAR</button></td> <td><button class="btn btn-success" >CREAR</button></td>
</tbody> </tbody>
</table> </table>
</form> </form>


Loading…
Cancel
Save