Taking request parameters via path variables

Spring MVC allows you to pass parameters in the URI instead of passing them through request parameters. The passed values can be extracted from the request URLs. It is based on URI templates. It is not a Spring-specific concept, and is used in many frameworks by using {...} placeholders and the @PathVariable annotation. It allows clean URLs without request parameters. The following is an example:

    @Controller 
    public class AccountController { 
      @GetMapping("/accounts/{accountId}") 
      public String show(@PathVariable("accountId") long accountId, Model model) { 
         Account account = accountService.findOne(accountId); 
         model.put("account", account); 
         return "accountDetails"; 
     } 
     ... 
   } 

In the previous handler, the method can handle the request like this:

http://localhost:8080/Chapter-10-Spring-MVC-pattern/account?accountId=1000 

But in the preceding example, the handler method can handle the request such as:

http://localhost:8080/Chapter-10-Spring-MVC-pattern/accounts/2000 

We have seen in the preceding code and images how to pass a value either by using request parameters or using path parameters. Both ways are fine if you are passing small amounts of data on a request. But in some cases, we have to pass a lot of data to the server, such as form submission. Let's see how to write controller methods that handle form submissions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset