Gleb Kogtev
Gleb Kogtev

Reputation: 111

Retrieving data from database using jsp (Hibernate + Spring + Maven)

Hello, guys. I'm trying to retrieve data from Oracle database, but nothing appears on the screen.

Here is my usersList.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Spring MVC Hello World</title>
</head>

<body>
    <h2>All Users in System</h2>

    <table border="1">
        <tr>
            <th>User Id</th>
            <th>Login</th>
        </tr>
        <c:forEach items="${users}" var="user">
            <tr>
                <td>${user.userId}</td>
                <td>${user.login}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

It seems to be ok. And here is Spring controller usersController.java

package com.controller;
import com.database.Users;
import com.service.UsersManager;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author glebk
 */
@Controller
@RequestMapping("/users")
public class UsersController {

    private static final Logger log = Logger.getLogger(UsersController.class);

    @Autowired
    UsersManager manager;

    @RequestMapping(value = "/getUsers", method = RequestMethod.GET)
    public String getAllEmployees(Model model)
    {
        List<Users> users = manager.getAllUsers();
        model.addAttribute("users", users);
        return "usersList";
    }
}

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet> 
        <servlet-name>event</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>event</servlet-name> 
        <url-pattern>/</url-pattern> 
    </servlet-mapping>
</web-app>

And event-servlet.xml

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">

    <tx:annotation-driven />
    <context:component-scan base-package="com" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"></property>  
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  
        <property name="username" value="admin"></property>  
        <property name="password" value="0000"></property>  
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>
                classpath:hibernate.cfg.xml
            </value>
        </property>
        <property name = "dataSource" ref = "dataSource"></property>
    </bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</beans>

When I try to put some message into my model, it is displayed. But with attribute "users" there is nothing. Please, help me. Thanks.

Upvotes: 1

Views: 14711

Answers (2)

Gleb Kogtev
Gleb Kogtev

Reputation: 111

I found out the answer. I just replaced

 <c:forEach items="${users}" var="user">
       <tr>
           <td>${user.userId}</td>
           <td>${user.login}</td>
       </tr>
 </c:forEach>

With

<c:forEach items="${users}" var="user">
      <tr>
          <td>
              <c:out value="${user.userId}"/>
          </td>
          <td>
              <c:out value="${user.login}" />
         </td>
     </tr>
</c:forEach>

And it works!

Upvotes: 2

Abdelhak
Abdelhak

Reputation: 8387

Try to write your component-scan like this:

   <context:component-scan base-package="com.controller" />

Upvotes: 0

Related Questions