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

Using a common prefix for the routes of a controller


In this recipe, we will define in one place a route prefix shared by all the routes of a controller. We will start the routes of the UserController controller with /user.

How to do it…

Here are the steps to set a route prefix:

  1. Add @RequestMapping with the common route prefix to the controller class:

    @Controller
    @RequestMapping("/user")
    public class UserController {
    ...
    }
  2. Add @RequestMapping with the remainder of the route to the controller methods:

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

How it works…

A request for the /user/add route will execute the addUser()method. A request for the /user/list route will execute the userList()method.