content top

Using the @PathVariable annotation in Spring MVC 3.0

Let’s say that you are creating an user account-based web application in Spring 3.0.  You would like to create a controller that would allow users to see each other’s profile by using a URL such as http://mysite/profile/username.  For example:

http://mysite/profile/Adam  would show user Adam’s profile.

http://mysite/profile/Baker would show user Baker’s profile.

Spring 3.0’s annotated controllers make this kind of mapping easy with the new @PathVariable mapping.

package com.technologicaloddity.hellowebapp;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ShowProfileController {

    @RequestMapping("/profile/{username}")
    public String showProfile(Model model, @PathVariable("username") String username) {
        model.addAttribute("username", username);
        return "showProfile"; // the view name
    }
    
}

As you can see, the RequestMapping for the showProfile method has something a bit unusual: {username}.  This is a PathVariable, and it means that this method will serve any request of the format "/profile/someUserName".  We capture the actual username in the next line using the @PathVariable annotation, and store it in the String username.  Now we can use it however we want!

Read More

Hello World Web App using Spring Annotations Part 6

In part 6, the final part of this series, we’ll create the View to display our message to the end-user.  We’ll also run the whole app under Tomcat and Sysdeo, and even show a bit of Java class hot-swapping.  Lastly, we will create a WAR file from our project.

If you have missed the previous parts of this series, you can find them here: part 1, part 2, part 3, part 4, part 5.  You’ll need to setup your environment from parts 1-4 if you want to follow along with the examples here.

You can download the full source of this project from http://technologicaloddity.com/code/general/HelloWebApp.zip.

Read More

Hello World Web App using Spring Annotations Part 5

In Part 5 of this series, we finally get to write some Java!  We’ll create a simple Spring controller using Annotations, using the web application requirements from part 1.

If you have missed the previous parts of this series, you can find them here: part 1, part 2, part 3, part 4.  You’ll need to setup your environment from parts 1-4 if you want to follow along with the examples here.

Read More

Hello World Web App using Spring Annotations Part 4

In part 4 of this series, we’ll finish off the configuration for the web application in Eclipse with Spring and Log4J configuration files.  We’ll also start the Tomcat server with Sysdeo and our dependencies loaded for the first time, and verify that Spring is working correctly.

Be sure you have set up Sysdeo Tomcat plugin and have your workspace setup as described if you want to follow along with the examples.  If you missed the previous parts, you can find them here: part 1, part 2, part 3.

Read More

Hello World Web App using Spring Annotations Part 3

This is part 3 of a series of posts describing a Hello World web application built using Eclipse, Maven, Sysdeo, and Spring Annotations.  In part 1, we used a Maven archetype to build the initial structure of our application, and configured it as a Sysdeo Tomcat project.  In part 2, we set up the web application’s web.xml file, including a line-by-line walkthrough.

In this third post, we will begin adding in the JAR files that our web application depends upon.  We’ll use Maven to add those dependencies.

Read More

Hello World Web App using Spring Annotations Part 2

In part 1 of this series, we set up a new web application project in Eclipse, using Maven and Sysdeo.

In this second of the Hello World web application posts, we’ll configure our application’s web.xml, and walk through the web.xml file line by line.

Read More
content top