4.4 Controlador ArticulosController.java
Actualizar applicationContext.xml
<?xml version=\'1.0\' encoding=\'UTF-8\' ?> <!-- was: <?xml version=\"1.0\" encoding=\"UTF-8\"?> --> <beans xmlns=\"http://www.springframework.org/schema/beans\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:p=\"http://www.springframework.org/schema/p\" xmlns:aop=\"http://www.springframework.org/schema/aop\" xmlns:tx=\"http://www.springframework.org/schema/tx\" xmlns:context=\"http://www.springframework.org/schema/context\" xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd \"> <context:annotation-config /> <bean id=\"crudDS\" class=\"org.springframework.jdbc.datasource.DriverManagerDataSource\" p:driverClassName=\"com.mysql.jdbc.Driver\" p:url=\"jdbc:mysql://localhost:3306/crud?zeroDateTimeBehavior=convertToNull\" p:username=\"root\" p:password=\"\" /> <bean id = \"jdbcTmpl\" class=\"org.springframework.jdbc.core.JdbcTemplate\"> <property name=\"dataSource\" ref = \"crudDS\" /> </bean> <bean id = \"articulosDao\" class = \"crud.model.ArticulosDao\"> <property name=\"jdbcTmpl\" ref = \"jdbcTmpl\" /> </bean> </beans>
Actualizar dispatcher-servlet.xml
Para que Spring reconozca las clases con anotaciones de forma automática, se debe agregar <context:component-scan base-package=\»\» /> en
<?xml version=\'1.0\' encoding=\'UTF-8\' ?> <!-- was: <?xml version=\"1.0\" encoding=\"UTF-8\"?> --> <beans xmlns=\"http://www.springframework.org/schema/beans\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:p=\"http://www.springframework.org/schema/p\" xmlns:aop=\"http://www.springframework.org/schema/aop\" xmlns:tx=\"http://www.springframework.org/schema/tx\" xmlns:context=\"http://www.springframework.org/schema/context\" xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd \"> <context:component-scan base-package=\"crud.controller\" /> <bean class=\"org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping\"/> <bean id=\"urlMapping\" class=\"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping\"> <property name=\"mappings\"> <props> <prop key=\"index.htm\">indexController</prop> </props> </property> </bean> <bean id=\"viewResolver\" class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\" p:prefix=\"/WEB-INF/jsp/\" p:suffix=\".jsp\" /> <bean name=\"indexController\" class=\"org.springframework.web.servlet.mvc.ParameterizableViewController\" p:viewName=\"index\" /> </beans>
ArticulosController.java
package crud.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import crud.model.Articulo; import crud.model.ArticulosDao; @Controller public class ArticulosController { @Autowired ArticulosDao dao; //-- Listar los artículos (Grid) @RequestMapping(value = \"/lista\") public String articuloview(Model m){ System.out.println(\"*** ArticulosController.articuloview\" ); List<Articulo> list = dao.getArticulos(); m.addAttribute(\"list\", list); return \"articulosGrid\"; } @RequestMapping(value = \"/nuevo\") public String showForm( Model m){ System.out.println(\"*** ArticulosController.showForm\" ); m.addAttribute(\"command\", new Articulo() ); return \"articuloNewForm\"; } @RequestMapping(value = \"/guardar\", method= RequestMethod.POST) public String guardar( @ModelAttribute(\"articulo\") Articulo articulo ){ System.out.println(\"*** ArticulosController.guardar\" ); dao.guardar(articulo); return \"redirect:lista.htm\"; } @RequestMapping(value = \"/editar\", method = RequestMethod.GET) public String actualiza (@RequestParam(value = \"id\", required = true) int id, Model m){ System.out.println(\"*** ArticulosController.actualiza id=\" + id); Articulo articulo = dao.getArticuloById( id); m.addAttribute(\"command\", articulo); return \"articuloEditForm\"; } @RequestMapping( value = \"/actualizar\", method = RequestMethod.POST) public String editsave( @ModelAttribute(\"articulo\") Articulo articulo) { System.out.println(\"*** ArticulosController.editsave \" ); dao.actualiza(articulo); return \"redirect:lista.htm\"; } @RequestMapping( value = \"/borrar\", method = RequestMethod.GET) public String borrar( @RequestParam(value = \"id\", required = true)int id) { System.out.println(\"*** ArticulosController.borrar id=\" + id ); dao.borrar(id); return \"redirect:lista.htm\"; } }
4.5 Vistas JSP
4.5.1 Vista para listar los artículos articulosGrid.jsp
<%@page contentType=\"text/html\" pageEncoding=\"UTF-8\"%> <%@ taglib uri=\"http://www.springframework.org/tags/form\" prefix=\"form\"%> <%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\"%> <!DOCTYPE html> <html> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> <title>Artículos View</title> </head> <body> <h1>Lista de Artículos!</h1> <table border=\"2\" width=\"70%\" cellpadding=\"2\"> <tr> <th>Id</th> <th>Nombre</th> <th>Clave</th> <th>Descripción</th> <th>Marca</th> <th>Editar</th> <th>Borrar</th> </tr> <c:forEach var=\"art\" items=\"${list}\"> <tr> <td>${art.idArticulo}</td> <td>${art.nombre}</td> <td>${art.cveArticulo}</td> <td>${art.descripcion}</td> <td>${art.marca}</td> <td><a href=\"editar.htm?id=${art.idArticulo}\">Editar</a></td> <td><a href=\"borrar.htm?id=${art.idArticulo}\">Borrar</a></td> </tr> </c:forEach> </table> <br/> <a href=\"nuevo.htm\">Agregar artículo</a> </body> </html>
Abrir el listado en el navegador
http://localhost:8080/HolaCrud/articulos/lista.htm
4.5.2 Vista para agregar un nuevo artículo articuloNewForm.jsp
<%@ taglib uri=\"http://www.springframework.org/tags/form\" prefix=\"form\"%> <%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\"%> <!DOCTYPE html> <html> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> <title>Agregar Artículo</title> </head> <body> <h1>Agregar Artículo!</h1> <form:form method=\"post\" action=\"guardar.htm\"> <table > <tr> <td>Clave : </td> <td><form:input path=\"cveArticulo\" /></td> </tr> <tr> <td>Nombre :</td> <td><form:input path=\"nombre\" /></td> </tr> <tr> <td>Descripción :</td> <td><form:input path=\"descripcion\" /></td> </tr> <tr> <td>Marca :</td> <td><form:input path=\"marca\" /></td> </tr> <tr> <td> </td> <td><input type=\"submit\" value=\"Guardar\" /></td> </tr> </table> </form:form> </body> </html>
4.5.3 Vista para editar un artículo articuloEditForm.jsp
<%@ taglib uri=\"http://www.springframework.org/tags/form\" prefix=\"form\"%> <%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\"%> <!DOCTYPE html> <html> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> <title>Formulario Editar Articulo</title> </head> <body> <h1>Formulario Editar Artículo!</h1> <form:form method=\"post\" action=\"actualizar.htm\"> <table > <tr> <td>Clave : </td> <td><form:hidden path=\"idArticulo\" /> <form:input path=\"cveArticulo\" /></td> </tr> <tr> <td>Nombre :</td> <td><form:input path=\"nombre\" /></td> </tr> <tr> <td>Descripción :</td> <td><form:input path=\"descripcion\" /></td> </tr> <tr> <td>Marca :</td> <td><form:input path=\"marca\" /></td> </tr> <tr> <td> </td> <td><input type=\"submit\" value=\"Actualizar\" /></td> </tr> </table> </form:form> </body> </html>
4.5.4 Modificar la página de inicio del proyecto redirect.jsp
<%@page contentType=\"text/html\" pageEncoding=\"UTF-8\"%> <% response.sendRedirect(\"articulos/lista.htm\"); %>