Ver Mensaje Individual
  #4 (permalink)  
Antiguo 07/05/2013, 14:54
Avatar de rgf1987
rgf1987
 
Fecha de Ingreso: diciembre-2012
Ubicación: Asturias
Mensajes: 269
Antigüedad: 11 años, 3 meses
Puntos: 22
Respuesta: Spring JDBCTemplate

Nada no hay manera, tengo la siguiente configuracion:

/WEB-INF/applicationContext.xml

Código XML:
Ver original
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.       xmlns:p="http://www.springframework.org/schema/p"
  5.       xmlns:aop="http://www.springframework.org/schema/aop"
  6.       xmlns:tx="http://www.springframework.org/schema/tx"
  7.       xmlns:context="http://www.springframework.org/schema/context"
  8.       xmlns:mvc="http://www.springframework.org/schema/mvc"
  9.       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10.       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  11.       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  12.       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  13.       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  14.       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  15.    
  16.    
  17.     <bean id="dataSource"
  18.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  19.  
  20.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  21.         <property name="url" value="jdbc:mysql://localhost:3306/todomotor" />
  22.         <property name="username" value="root" />
  23.         <property name="password" value="root" />
  24.     </bean>
  25.    
  26.     <bean id="IUsuarioDAO" class="dao.UsuarioDAO">
  27.         <property name="dataSource" ref="dataSource"/>
  28.     </bean>    
  29.    
  30.                    
  31. </beans>


/WEB-INF/dispatcher-servlet.xml
Código XML:
Ver original
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.       xmlns:p="http://www.springframework.org/schema/p"
  5.       xmlns:aop="http://www.springframework.org/schema/aop"
  6.       xmlns:tx="http://www.springframework.org/schema/tx"
  7.       xmlns:context="http://www.springframework.org/schema/context"
  8.       xmlns:mvc="http://www.springframework.org/schema/mvc"
  9.       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10.       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  11.       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  12.       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  13.       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  14.       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  15.    
  16.     <context:component-scan base-package="controladores"/>
  17.      
  18. </beans>

/WEB-INF/web.xml

Código XML:
Ver original
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  3.   <display-name>Prueba</display-name>
  4.   <welcome-file-list>    
  5.     <welcome-file>index.htm</welcome-file>
  6.     <welcome-file>index.jsp</welcome-file>
  7.     <welcome-file>default.html</welcome-file>
  8.     <welcome-file>default.htm</welcome-file>
  9.     <welcome-file>default.jsp</welcome-file>
  10.   </welcome-file-list>
  11.   <context-param>
  12.     <param-name>contextConfigLocation</param-name>
  13.     <param-value>/WEB-INF/applicationContext*.xml</param-value>
  14.   </context-param>
  15.   <listener>
  16.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  17.   </listener>
  18.   <servlet>
  19.         <servlet-name>dispatcher</servlet-name>
  20.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  21.         <load-on-startup>2</load-on-startup>
  22.     </servlet>    
  23.     <servlet-mapping>
  24.         <servlet-name>dispatcher</servlet-name>
  25.         <url-pattern>*.htm</url-pattern>
  26.     </servlet-mapping>
  27. </web-app>


Y luego tengo un controlador de la siguiente manera:
Código JAVA:
Ver original
  1. @Controller
  2. @SessionAttributes({"usuario"})
  3. public class Controlador {
  4.         ManagerGeneral mg = new ManagerGeneral();
  5.         UsuarioVO usuario;      
  6.                
  7.         @RequestMapping("index.htm")
  8.     public String index(ModelMap modelo) {
  9.             List<UsuarioVO> listaUsuarios=null;
  10.             try {
  11.                 listaUsuarios = mg.getUsuariosRevisores();
  12.             } catch (SQLException e) {
  13.                 // TODO Auto-generated catch block
  14.                 e.printStackTrace();
  15.             }          
  16.             modelo.addAttribute("listaUsuarios", listaUsuarios);  
  17.            
  18.             return Constantes.raiz +"prueba.jsp";
  19.     }        
  20.        
  21. }

Interfaz DAO

Código JAVA:
Ver original
  1. public interface IUsuarioDAO {
  2.     public List<UsuarioVO> getUsuariosRevisores() throws SQLException;
  3.     public void setDataSource(DataSource dataSource);
  4. }

y su implementacion

Código JAVA:
Ver original
  1. public class UsuarioDAO implements IUsuarioDAO{
  2.    
  3.    
  4.     private DataSource dataSource;
  5.     private JdbcTemplate jdbcTemplate;
  6.    
  7.     public void setDataSource(DataSource dataSource) {
  8.  
  9.         // se instancia plantilla con la inyeccion del datasource
  10.         this.dataSource = dataSource;
  11.         this.jdbcTemplate = new JdbcTemplate(this.dataSource);
  12.        
  13.  
  14.     }
  15.    
  16.     /**
  17.      * Devuelve un listado de los usuarios que pueden realizar funciones de revisor.
  18.      * @return
  19.      * @throws SQLException
  20.      */
  21.     public List<UsuarioVO> getUsuariosRevisores() throws SQLException {
  22.        
  23.         if(this.jdbcTemplate==null){
  24.             System.out.print("JDBC TEMPLATE IS NULL");
  25.         }else{
  26.             System.out.print("JDBC TEMPLATE IS NOT NULL "+jdbcTemplate);
  27.         }
  28.        
  29.         StringBuffer sql = new StringBuffer("SELECT * FROM USUARIOS WHERE nivelUsuario<'4'");            
  30.            
  31.         List<UsuarioVO> listaUsuarios = this.jdbcTemplate.query(sql.toString(),
  32.                 new RowMapper<UsuarioVO>() {
  33.             @Override
  34.             public UsuarioVO mapRow(ResultSet rs, int arg1) throws SQLException {
  35.                // Se rellena un bean Articulo a partir de la fila actual
  36.                // del ResultSet
  37.                    UsuarioVO usuario = new UsuarioVO();
  38.                    usuario.setIdUsuario(rs.getInt("idUsuario"));
  39.                    usuario.setNombreUsuario(rs.getString("nombreUsuario"));
  40.                    usuario.setApellidosUsuario(rs.getString("apellidosUsuario"));
  41.                    usuario.setNickUsuario(rs.getString("nickUsuario"));
  42.                    usuario.setContrasUsuario(rs.getString("contrasUsuario"));
  43.                    usuario.setEmailUsuario(rs.getString("emailUsuario"));
  44.                    usuario.setNivelUsuario(rs.getInt("nivelUsuario"));
  45.                    return usuario;
  46.             }});
  47.         return listaUsuarios;      
  48.        
  49.     }  
  50.    
  51.  
  52. }




Me sale un error 404, y nose porque :(

si modifico el bean IUsuarioDAO del applicationContext y lo dejo asi:

Código XML:
Ver original
  1. <bean id="IUsuarioDAO" class="dao.UsuarioDAO"/>

La pagina me carga, pero me sale un error NULLPOINTEREXCEPTION porque la variable jdbcTemplate esta a null.

Ya no se que hacer, ¿alguien puede echarme una mano??! Estoy desesperado ya, no consigo avanzar!!