title: Spring, Hibernate retrieving simple user list from database on jsp page
body: <p>i'm beginner with spring hibernate. Need to show users from database simple on jsp page with EL, got no errors, app started, classes mapped into database but nothing shows on jsp page, only table "User Name" from th. Maybe input or anotations are wrong cant see, don't know. Table in MySQL database is user with two users already inside. Thank You</p>
  
  <p>users jsp page</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt;
  &lt;%@include file="/WEB-INF/view/header.jsp" %&gt;
  
  &lt;table class="table table-bordered table-hover table-striped"&gt;
      &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;User Name&lt;/th&gt;
      &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      &lt;c:forEach items="${users}" var="user"&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;c:out value="${user.name}"/&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/c:forEach&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;%@include file="/WEB-INF/view/footer.jsp" %&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Controller</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>@Controller
  public class UserController {
  
      @Autowired
      private UserService userService;
  
      @RequestMapping("/users")
      public String users(Model model){
          List&lt;User&gt; users = userService.getUserList();
          model.addAttribute("users", users);
          return "users";
      }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>UserDao</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>@Transactional
  @Repository
  public class UserDao {
  
      @Autowired
      SessionFactory sessionFactory;
  
      public List&lt;User&gt; getUserList() throws NullPointerException {
          List&lt;User&gt; userList = null;
          try {
              Session session = sessionFactory.getCurrentSession();
              String hql = "FROM com.hellospringdemo.model.User";
              Query query = session.createQuery(hql);
              userList = query.list();
              session.flush();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return userList;
  
      }
  }
  Tried with this also same show's nothing...
  Session session = sessionFactory.getCurrentSession();
  List&lt;User&gt; result = session.createCriteria(User.class).list();
  return result;</code></pre>
  </div>
  </div>
  </p>
  
  <p>UserService from interface</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>@Service
  public class UserServiceImpl implements UserService {
  
      @Autowired
      private UserDao userDao;
  
      public List&lt;User&gt; getUserList() {
          return userDao.getUserList();
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>UserService interface</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public interface UserService {
  
      List&lt;User&gt; getUserList();
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Dispatcher</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;context:component-scan base-package="com.hellospringdemo"/&gt;
  
      
      &lt;mvc:annotation-driven/&gt;
  
     
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/WEB-INF/view/"/&gt;
          &lt;property name="suffix" value=".jsp"/&gt;
      &lt;/bean&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>AppContext.xml</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;context:component-scan base-package="com.hellospringdemo"/&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="url" value="jdbc:mysql://localhost/blog"/&gt;
          &lt;property name="username" value="root"/&gt;
          &lt;property name="password" value=""/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
  
          &lt;property name="packagesToScan"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.hellospringdemo&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
  
      &lt;/bean&gt;
  
  
      
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven/&gt;
  
  
      &lt;bean id="userDao" class="com.hellospringdemo.model.UserDao"/&gt;
      
  
  &lt;/beans&gt;</code></pre>
  </div>
  </div>
  </p>