Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @GetMapping("/nuevo")
- public static String nuevoGet() {
- return "nuevoProducto";
- }
- @PostMapping("/nuevo")
- public static String nuevoPost(@RequestParam String nombre,
- @RequestParam String descripcion, @RequestParam String precio,
- @RequestParam String tipo, @RequestParam String imagen, Model template) throws SQLException {
- if (nombre.equals("") || descripcion.equals("") || precio.equals("")
- || tipo.equals("") || imagen.equals("")) {
- template.addAttribute("nombre", nombre);
- template.addAttribute("descripcion", descripcion);
- template.addAttribute("precio", precio);
- template.addAttribute("tipo", tipo);
- template.addAttribute("imagen", imagen);
- template.addAttribute("MensajeError",
- "Disculpe, hubo un error en la carga de su producto, por favor, revise los datos");
- return "nuevoProducto";
- }else{
- Connection connection;
- connection = DriverManager.getConnection(
- "jdbc:postgresql://localhost:5432/SakuraCakes", "postgres",
- "admin");
- PreparedStatement ps = connection
- .prepareStatement("INSERT INTO productos(nombre, descripcion, precio, tipo, imagen)"
- + "VALUES(?, ?, ?, ?, ?);");
- Double p = Double.parseDouble(precio);
- ps.setString(1, nombre);
- ps.setString(2, descripcion);
- ps.setDouble(3, p);
- ps.setString(4, tipo);
- ps.setString(5, imagen);
- ResultSet result = ps.executeQuery();
- Producto d = new Producto(result.getString("nombre"),
- result.getString("descripcion"),
- result.getDouble("precio"), result.getString("tipo"), result.getString("imagen"));
- template.addAttribute("productos", d);
- return "listo";
- }
- }
- <form action="/nuevo" method="POST" id="form">
- <label for="nombre">Nombre</label>
- <input name="nombre" type="text" placeholder="nombre..." th:value="${nombre}"></input>
- <label for="descripcion">Descripcion</label>
- <textarea name="descripcion" class="form-control" placeholder="Describe tu producto..." th:value="${descripcion}" rows="5" maxlength="300"></textarea>
- <label for="precio">Precio</label>
- <input name="precio" type="text" placeholder="precio..." th:value="${precio}"></input>
- <label for="tipo">Tipo</label>
- <input name="tipo" type="text" placeholder="tipo..." th:value="${tipo}"></input>
- <label for="imagen">Imagen</label>
- <input id="inputUrlImagen" name="imagen" type="text" th:value="${imagen}"></input>
- <button id="botonSubirImagen" type="button">Subir Imagen</button>
- <br/><br/><br/>
- <input type="submit" id="boton" value="Enviar"></input>
- <label></label><p th:text="${MensajeError}"></p>
- </form>
- $(document).ready( function(){
- $("#boton").click(function(ev){
- ev.preventDefault();
- $('#form').submit();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement