In the first part of this tutorial, we got the web application set up so that we can start building on it.

Stubbing out classes

In the xml files, we have referenced three classes by this point that we will need to stub out so that our application will run.

  • Entry
  • EntryRepository
  • HomeController


@Table(name = "entry") //because I always forget about case sensitivity between Linux and Windows
public class Entry {

	@Id @GeneratedValue(strategy = GenerationType.AUTO) 
        protected Long id;

	private String title;
	private String text;

	//getters and setters...


For now we just need a way of getting entries from the database. We’ll put in the other method later.

@Transactional(readOnly = true)
public class EntryRepository {

   protected SessionFactory sessionFactory;
   public List getAll() {
      return sessionFactory


On the home page lets simply list the titles of all Entry object in the database.

public class HomeController extends ParameterizableViewController {

   @Autowired private EntryRepository entryRepository;

   @RequestMapping(method = RequestMethod.GET)
   public String showPage(   HttpServletRequest request,
                             HttpServletResponse response,
                             ModelMap model) {

      model.put("entrylist", entryRepository.getAll());
      return getViewName();

Template file for the HomeController

We told out HomeController to expect a file named index.html to be available. So lets make this file and put it in the webapp directory.

index.html (in the webapps folder)

  Sandbox - Hibernate and Spring


Create an entry


[#list entrylist as item]




You will need to follow some instructions for setting this up to run with a tool such as maven or ant. I haven’t posted instructions here yet, but you should be able to find them easily enough on the net.

Once these classes are in place, lets build the project and run jetty.

Running jetty

When the application runs for the first time, the database tables will get created. So when we browse to our application and hit the /index page, we will see our page but there will be no entries listed. You can add some entries manually into your database to see that they will be loaded by the controller.

So what have we accomplished so far?

  1. Set up the application xml files
  2. Created an entity called Entry and a repository to control how it is loaded from the database
  3. Added a controller and a template so that we can view a list of entries.

What is happening when I browse to /index?

A sequence of events starts off from the servlet that we mapped in the web.xml file…

  1. Our servlet is listening for requests at the / url, so the request for /index is dispatched to our servlet
  2. This request is then passed into the PathParameterFilter (web.xml) and into the URLHandlerMapping that we set up in spring-servlet.xml
  3. The mapping for /index is found to be HomeController so control is passed to the HomeController bean
  4. At the same time, a Hibernate session is opened for us by the OpenSessionInViewInterceptor (app-context.xml) and a transaction manager is wired in to our session factory
  5. HomeController’s method that is mapped by the GET annotation is then run
  6. This load all Entry objects from the database and places them on the model
  7. The view name is loaded from the bean definition in the spring-servlet.xml file
  8. Freemarker then locates the template and loads it up with the model data
  9. The page is then rendered
  10. The session is closed

Moving forward

Now that we have our application basics set up, we can move on to Part 3 of the tutorial where we will look at GET and POST methods of the annotated controllers.