Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Associating a route to a controller method


In this recipe, you will learn how to define a controller method to be executed for a given route.

How to do it…

Here are the steps for creating a controller method for a given route:

  1. Create a controller class in your controller package (for example, com.springcookbook.controller). This is a normal Java class annotated with @Controller:

    @Controller
    public class UserController {
    ...
    }
  2. Add a controller method. This is a standard Java method annotated with @RequestMapping, which takes the route as a parameter:

    @RequestMapping("/user/list")
    public void userList() {
    ...
    }

How it works…

A request with the /user/list route will execute the userList()method.