Path Binding with Controller

The Controller is mapped with URL with the help of @RequestMapping annotation. Annotation @RequestMapping can map Controller class and its methods as well. Mapping can be further bound with HTTP POST and GET methods.

Typically the class-level annotation maps a specific request path (or path pattern) to the class controller, with additional method-level annotations narrowing the primary mapping for a specific HTTP method request method ("GET", "POST", etc.) or an HTTP request parameter condition.

Bind Request Path to the Controller and GET and POST requests to its methods

Here is sample code that binds UrlMappingCtl class with path "/UrlMapping" . Further it binds GET request to display() method and POST to submit() method. You can access this controller using GET request by making a call at url http://localhost:8080/STMavenSpringMVC/UrlMapping.

@Controller

@RequestMapping(value = "/UrlMapping")

public class UrlMappingCtl {

//Map GET request with display() method.

@RequestMapping(method = RequestMethod.GET)

public String display() {

System.out.println("I am in display method");

return "UrlMappingView";

}

//Map GET request with display() method.

@RequestMapping(method = RequestMethod.POST)

public String submit() {

System.out.println("I am in submit method");

return "UrlMappingView";

}

This View is resolved by ViewResolver as /WEB-INF/pages/UrlMappingView.jsp. Here is form that sends POST request.

<f:form action="UrlMapping" method="post">

<input type="submit" value="Send Post Request" name="operation">

</f:form>

Bind sub-path to method of the Controller

You can map sub-path to the method. In order to make a call to path bound method, sub-path will be prefixed by class level path.

Here is sample code that maps path \search to method search(). Further method is bound to GET and POST requests both.

@RequestMapping(value = "/search", method = { RequestMethod.POST, RequestMethod.GET })

public String search() {

System.out.println("I am in search method, it handles GET and POST methods.");

return "TestSearchView";

}

Request can be made to this method by calling http://localhost:8080/STMavenSpringMVC/UrlMapping/search using either GET and POST request.

Bind request parameters to method parameters

You can bind request parameters to method parameters with the help of @RequestParam annotation. In order to bind a request parameter, its name must be same as method parameter. You can bind one or multiple request parameters to method.

Here is the example code we are binding "message" request parameter to method parameter with help of @RequestParam annotation.

@RequestMapping(value = "/param", method = RequestMethod.GET)

public String requestParam(@RequestParam String message) {

System.out.println("Request Parameter : " + message);

return "UrlMappingView";

}

FAQ

Q: How do you bind sub URLs with a method of the controller?

Q: How do you bind HTTP GET and HTTP POST method with a method of the controller?

Q: How do you bind request parameters with method parameters?

Q: How do you bind request parameters with Form bean?

Q: How do you bind URI variables with method parameters?

Q: How do you bind JSON request body object to Form bean?