title: List objects from database using a Spring controller
body: <p>I'm developing a website using a Java Spring framework. It is an internet forum. Now, after logging in, I would like to list all threads that are currently stored in my database. I will walk you through the important part of my implementation to date first. The post is quite long, but don't get discouraged, it really is a simple, elementary problem, that anyone with rudimentary experience in web programming can solve in no time. I promise you that.</p>
  
  <p>The Thread object is defined by its own class Thread. It's a standard Entity class with some fields, constructors and getters/setters.</p>
  
  <pre><code>@Entity
  public class Thread {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      int _id;
  
      private Date last_modified;
      private int topics;
      private String description;
      private String name;
  
      public Thread(){
  
      }
  
  
  
      public Thread(int _id, Date last_modified, int topics, String description,
              String name) {
          super();
          this._id = _id;
          this.last_modified = last_modified;
          this.topics = topics;
          this.description = description;
          this.name = name;
      }
  
  
  
      public int getId() {
          return _id;
      }
      public void setId(int _id) {
          this._id = _id;
      }
      public Date getLast_modified() {
          return last_modified;
      }
      public void setLast_modified(Date last_modified) {
          this.last_modified = last_modified;
      }
      public int getTopics() {
          return topics;
      }
      public void setTopics(int topics) {
          this.topics = topics;
      }
      public String getDescription() {
          return description;
      }
      public void setDescription(String description) {
          this.description = description;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
  
  }
  </code></pre>
  
  <p>Now, moving on to the next layer, I have the corresponding DAO interface in my DAO package:</p>
  
  <pre><code>@Transactional(propagation = Propagation.REQUIRED)
  public interface ThreadDAO {
  
      public void addThread(Thread thread);
      public List&lt;Thread&gt; listThread();
      public void removeThread (int id);
      public void editThread(Thread thread);
  
  
  }
  </code></pre>
  
  <p>and its implementation:</p>
  
  <pre><code>@Repository("threadDao")
  @Transactional
  public class ThreadDAOImpl implements ThreadDAO{
  
      @Autowired
      SessionFactory sessionFactory;
  
  
  
      @Override
      public void addThread(Thread thread) {
          // TODO Auto-generated method stub
          sessionFactory.getCurrentSession().save(thread);    
      }
  
      @Override
      public List&lt;Thread&gt; listThread() {
          // TODO Auto-generated method stub
          return sessionFactory.getCurrentSession().createQuery("from Thread order by _id").list();
      }
  
      @Override
      public void removeThread(int id) {
          // TODO Auto-generated method stub
           Thread thread = (Thread) sessionFactory.getCurrentSession().load(
                      Thread.class, id);
              if (null != thread) {
                  sessionFactory.getCurrentSession().delete(thread);
              }
      }
  
      @Override
      public void editThread(Thread thread) {
          // TODO Auto-generated method stub
          sessionFactory.getCurrentSession().update(thread);
      }
  
  }
  </code></pre>
  
  <p>In order to push data to my controller, I use an autowired service: </p>
  
  <pre><code>@Transactional(propagation = Propagation.REQUIRED)
  public interface ThreadService {
  
      public void addThread(Thread thread);
      public List&lt;Thread&gt; listThread();
      public void removeThread (int id);
      public void editThread(Thread thread);
  
  
  }
  </code></pre>
  
  <p>implementation:</p>
  
  <pre><code>@Service("threadService")
  @Transactional
  public class ThreadServiceImpl implements ThreadService{
  
  
      @Autowired
      ThreadDAO threadDAO;
  
      @Override
      public void addThread(Thread thread) {
          // TODO Auto-generated method stub
          threadDAO.addThread(thread);
      }
  
      @Override
      public List&lt;Thread&gt; listThread() {
          // TODO Auto-generated method stub
          return threadDAO.listThread();
      }
  
      @Override
      public void removeThread(int id) {
          // TODO Auto-generated method stub
          threadDAO.removeThread(id);
      }
  
      @Override
      public void editThread(Thread thread) {
          // TODO Auto-generated method stub
          threadDAO.editThread(thread);
      }
  
  }
  </code></pre>
  
  <p>Finally, I should execute Service methods in the controller:</p>
  
  <pre><code>@Controller
  public class LoginController {
  
      @Autowired
      UserService userService;
  
      @Autowired
      ThreadService threadService;
  
      @RequestMapping(value = "/login", method = RequestMethod.GET)
      public ModelAndView login(
          @RequestParam(value = "error", required = false) String error,
          @RequestParam(value = "logout", required = false) String logout) {
  
          ModelAndView model = new ModelAndView();
          if (error != null) {
              model.addObject("error", "Invalid username and password!");
          }
  
          if (logout != null) {
              model.addObject("msg", "You've been logged out successfully.");
          }
          model.setViewName("login");
  
          return model;
  
      }
  
  
  
      @RequestMapping(value = "/standard", method = RequestMethod.GET)
      public ModelAndView standardView() 
      {
  
          ModelAndView model = new ModelAndView();
          model.setViewName("standard");
          return model; 
  
      }
  }
  </code></pre>
  
  <p>And that's where my problems begin. Because I'm terrible at web programming in general, and the concept of request mapping is ever-confusing to me. The 'standard' layout contains for now the information, that the user is logged in, and displays it's username, plus I added the list layout for my threads:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;
  &lt;%@taglib uri="http://www.springframework.org/tags" prefix="spring"%&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Access denied&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;!-- h1&gt;&lt;spring:message code="access.denied"/&gt;&lt;/h1&gt; --&gt;
  &lt;h6&gt;Logged in as: ${pageContext.request.userPrincipal.name}&lt;/h6&gt;
  
  &lt;c:url value="/j_spring_security_logout" var="logoutUrl" /&gt;
  
      &lt;!-- csrt for log out--&gt;
      &lt;form action="${logoutUrl}" method="post" id="logoutForm"&gt;
        &lt;input type="hidden" 
          name="${_csrf.parameterName}"
          value="${_csrf.token}" /&gt;
      &lt;/form&gt;
      &lt;script&gt;
          function formSubmit() {
              document.getElementById("logoutForm").submit();
          }
      &lt;/script&gt;
  
  &lt;a href="javascript:formSubmit()"&gt; Logout&lt;/a&gt;
  
  &lt;h3&gt;&lt;spring:message code="label.threadList"/&gt;&lt;/h3&gt;
  &lt;c:if  test="${!empty threadList}"&gt;
  &lt;table class="data"&gt;
  &lt;tr&gt;
      &lt;th&gt;&lt;spring:message code="label.threadName"/&gt;&lt;/th&gt;
       &lt;th&gt;&lt;spring:message code="label.threadDescription"/&gt;&lt;/th&gt;
      &lt;th&gt;&lt;spring:message code="label.threadTopicCount"/&gt;&lt;/th&gt;
      &lt;th&gt;&lt;spring:message code="label.threadLastModified"/&gt;&lt;/th&gt;
  
  &lt;/tr&gt;
  &lt;c:forEach items="${threadList}" var="thread"&gt;
      &lt;tr&gt;
          &lt;td&gt;${thread.name} &lt;/td&gt;
          &lt;td&gt;${thread.description} &lt;/td&gt;
          &lt;td&gt;${thread.topics}&lt;/td&gt;
          &lt;td&gt;${thread.last_modified}&lt;/td&gt;
  
      &lt;/tr&gt;
  &lt;/c:forEach&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>What is missing, is the request mapping in the controller. How can I map it, so that it passes the Thread entries, record by record, and display them in this layout, along the login information?</p>