Creating a controller and view

mNow, let's do some real Spring MVC programming. We have deserved it after all this set up and configuration. First we are going to create a Controller and after that we will create a View to render some text provided by the controller. Many of you would call this a 'hello world' example and many of you would be right :)

Creating the controller

  1. If you hadn't already done so, create a package to hold you controllers in the src/main/java folder. As you can see at the bottom of the previous page, I have chosen for net.tekstenuitleg.springmvc.controlller. I can imagine you will choose something different.

    Just make sure that your package conforms to the value you provided earlier in the context:component-scan tag in application-context.xml. If your package name is not lined up with the component-scan base directory, than Spring won't find your controllers and you will fail miserably and you will have to go back in time to prevent your other self from making this mistake. 
  2. Ok, now create a new class called AccountController inside your new package and add the following content to it.
    package net.tekstenuitleg.springmvc.controller; // change this to reflect your package structure
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    public class AccountController {
        @RequestMapping(value = "/account", method = RequestMethod.GET)
        public ModelAndView findAllAccounts() throws Exception {
            ModelAndView mav = new ModelAndView();
            mav.addObject("someText", "Listing all accounts!");
            return mav;
        @RequestMapping(value="/{accountId}", method = RequestMethod.GET)
        public ModelAndView findAccount(@PathVariable int accountId, Model model) {
            ModelAndView mav = new ModelAndView();
            mav.addObject("someText", String.format("Showing account %d", accountId));
            return mav;
  3. Create a file called account.jsp in the /webapp/WEB-INF/views folder and add the following content to it.
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="" %>
  4. Now, run the Clean package Maven command that you configured earlier.

    Eclipse run menu
  5. Open the target folder inside your main project folder and find the WAR  that Maven created and deploy it to Tomcat.
  6. Now test your MVC set up by opening your browser and visiting the following URLs.


    spring MVC result

Well, that's it. Our Spring MVC set up works.

  • The Spring dispatcher servlet handled our request.
  • Spring's component-scan has discovered our controller, because it is annotated with the @Controller annotation.
  •  Also, the @RequestMapping annotation caused the /account and account/123 URLs to match our two controller methods. 
  • And finally Spring's view resolver discovered our view JSP (account.jsp) by reconstructing its complete path, based on the name we provided ('account').

Isn't that sweet :) But there a few things to take care of still. Right now, deploying the application is a tedious manual job. That is why we are going to make this easier in the following pages. Finally we will look into JPA and Hibernate.