Introduction to Spring MVC

In this training, we will introduce Spring MVC. This training is continued in Spring MVC - Part 2 and Spring MVC - Part 3.


We apologize for the sub-par sound quality. We had some recording issues with this training.


Prerequisites for this course include: Introduction to the Java Stack, Introduction to Spring, Introduction to Spring (part2), Introduction to Web App Development, Introduction to Java Server Pages (JSP), and Intermediate Java Server Pages (JSP). You should also have an IDE (Eclipse, IntelliJ, or NetBeans) and know how to use a Stack project with it.


Download presentation slides.

Presentation, Part 1

Get Adobe Flash player

Lab 1

1. Create a project with Java Stack Starter, changing the following options:

  1. Give it the name mvc-training (you can give it a different name, but you'll need to adjust future steps accordingly)
  2. Security tab: Uncheck 'Use LDS Account Authentication'
  3. Service Layer tab: Uncheck 'Use a database?'
  4. View Layer tab: View > Choose 'Manual Configuration'

2. Import your new project into your IDE of choice and associate it with a Tomcat server. If you are unsure about how to do this, see our guides for doing so in Eclipse, IntelliJ, or NetBeans.

3. Add the Spring MVC dependency to your web pom:


We don't have to specify a version because a version for this dependency is specified in the dependencyManagement section of stack-thirdparty (which this project extends).

4. Add the required configuration to your web.xml:



5. Create a yourapp-servlet.xml file in src/main/webapp/WEB-INF and add the following:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns=""
        <context:component-scan base-package="" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />

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

6. Spring MVC should be configured properly, so now it is time to add a simple "Hello, World" page. Start by adding the mapping for it in yourapp-servlet.xml:

    <mvc:view-controller path="/hello" view-name="hello" />

7. Now add a view for it in src/main/webapp/WEB-INF/views/hello.jsp. This file can just contain plain text.

8. Start your application up and navigate to http://localhost: 8080/mvc-training/hello

Solution, Part 1

Get Adobe Flash player

Presentation, Part 2

Get Adobe Flash player

Lab 2

In this lab, we'll define annotation-based controllers and use a few different request mappings. The content of the JSP files is left up to you.

1. Remove the following line from your yourapp-servlet.xml:

    <mvc:view-controller path="/hello" view-name="hello" />

2. Add a controller class: and annotate it with @Controller and @RequestMapping("/hello")


    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    public class HelloController {


3. Add our hello method so it is available at /hello and returns the hello view (which can be found in src/main/webapp/WEB-INF/views/hello.jsp).

    public String hello() {
        return "hello";

4. Verify that everything still works by going to http://localhost: 8080/mvc-training/hello in your browser.

5. Now let's add a goodbye method so that going to /hello/goodbye will tell the user 'Goodbye!'

    public String goodbye() {
        return "goodbye";

Remember to add the view for it in src/main/webapp/WEB-INF/views/goodbye.jsp and make sure it works in your browser (http://localhost: 8080/mvc-training/hello/goodbye).

6. Finally, let's make the 'goodbye' functionality a little bit smarter by returning a slightly different message (See you later!) if they are just temporarily leaving. We can do this by adding another controller that looks at a request parameter (temporary) and returns 'seeyou' if that parameter is true.

    @RequestMapping(value = "/goodbye", params="temporary=true")
    public String seeyou() {
        return "seeyou";

Verify that it works by going to: http://localhost: 8080/mvc-training/hello/goodbye?temporary=true

Extra Credit

1. Create another mapping that only responds to DELETE requests.

2. Create another mapping that only responds if the Content-Type is set to application/json.

Solution, Part 2

Get Adobe Flash player

Presentation, Part 3

Get Adobe Flash player

Lab 3

In this lab, we'll use everything we've learned in this training to create a simple form that utilizes @RequestMapping features, as well as the ability to pass values back to the view.

1. Create your registration controller so that all requests for this controller go to /registration.


    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    public class RegistrationController {


2. Now let's add the register page at /registration/view/{eventId} and echo the event ID back to the user. In this case, we'll do it using the ModelMap.

    public String view(@PathVariable("eventId") Long id, ModelMap model) {
        model.addAttribute("eventId", id);
        return "registration/view";

3. We need to add a jsp to src/main/webapp/WEB-INF/views/registration/view.jsp. This will simply echo back the event id for the user.

    Registering for ${eventId}.

4. Verify that everything works by visiting a few different URLs in your browser:

http://localhost: 8080/mvc-training/registration/view/2

Should result in a page that says "Registering for 2."

http://localhost: 8080/mvc-training/registration/view/896

Should result in a page that says "Registering for 896."

and so on...

5. Now let's enhance this so we can do a simple signup. Modify your view.jsp so it has a form that posts back to itself:

    Registering for ${eventId}.
    <br /><br />
    <form method="POST">
        Name: <input type="text" name="name" />
        <br /><br />
        Email: <input type="text" name="email" />
        <br /><br />
        <input type="submit" value="Signup" />

6. Now create the controller method for it. We'll use the exact same URL (including the URI template), but will specify that it must be a POST. We'll use a ModelAndView in this example, and we'll pass in an HttpServletRequest object, as well as a specific header (the User-Agent):

    @RequestMapping(value = "/view/{eventId}", method=RequestMethod.POST)
    public ModelAndView register(@PathVariable("eventId") Long id,
            @RequestHeader("user-agent") String userAgent,
            HttpServletRequest request) {
        ModelAndView model = new ModelAndView("registration/confirm");
        model.addObject("eventId", id);
        model.addObject("name", request.getParameter("name"));
        model.addObject("email", request.getParameter("email"));
        model.addObject("userAgent", userAgent);
        return model;

7. Now let's reference these values we are adding in our view (src/main/webapp/WEB-INF/views/registration/confirm.jsp):

    You are now registered for event: ${eventId}!
    <br /><br />
    Your information:
    <br /><br />
    Name: ${name}
    <br /><br />
    Email: ${email}
    <br /><br /><br /><br />
    You registered using: ${userAgent}

8. Now see if your registration works:

http://localhost: 8080/mvc-training/registration/view/2

Extra Credit

1. Make all of the output safe from XSS attacks using the processes documented here and here.

2. Return a different view if the user is registering with an email address.

Solution, Part 3

Get Adobe Flash player
This page was last modified on 12 July 2013, at 19:30.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.