官术网_书友最值得收藏!

The PathVariable annotation

In this section, we will learn how to use the PathVariable annotation for handling request parameters.

The PathVariable annotation is used to bind a method parameter to a URI template variable. A URI template is a URI-like string containing one or more variables. For example, in  the following code, /{nickname} is a URI template consisting of nickname as a variable. A method can have multiple @Pathvariable annotations to bind multiple variables present in the URI template. PathVariable is seen to be mainly used in the REST web service. Later, while going through Angular chapters, you will see that PathVariable is very similar to handling routing parameters using the ActivatedRoute concept:

    @Controller
@SpringBootApplication
public class HealthAppApplication {

@RequestMapping("/")
public String home() {
return "index";
}

@GetMapping("/{nickname}")
public String home(ModelMap model, @PathVariable String nickname) {
model.addAttribute("name", nickname);
return "index";
}

public static void main(String[] args) {
SpringApplication.run(HealthAppApplication.class, args);
}
}

Both RequestParam and PathVariable can be used to read the request parameters. @RequestParam is used to retrieve the query parameters from the request URL. On the other hand, @PathVariable is used to retrieve one or more placeholders from the URI. URLs without query parameters, for example, paths, tend to be cached. The following code demonstrates the usage of both RequestParam and PathVariable. Different URLs will be handled using different methods represented in the code as follows:

  • URL (http://localhost:8080, localhost:8080/?name=calvin): This URL consists of a parameter such as name. The value of this parameter is obtained using RequestParam. Note that as required = false is declared with the @RequestParam definition in the code example given next, thus, a request URL such as http://localhost:8080/ would also get mapped to the usingRequestParam method. 
  • URI (http://localhost:8080/calvin): This URL consists of a name which can be handled using a placeholder variable whose value can be obtained using the PathVariable method.

The following code displays the usage of both RequestParam and PathVariable:

    @Controller
@SpringBootApplication
public class HealthAppApplication {
//
// RequestParam is used to retrieve value of parameter, name.
//
@RequestMapping("/")
public String usingRequestParam(Model model,
@RequestParam(value="name", required=false) String nickname) {
model.addAttribute("nickname", nickname);
return "index";
}

@RequestMapping("/{nickname}")
public String usingPathVariable(Model model, @PathVariable String nickname)
{
model.addAttribute("nickname", nickname);
return "index";
}
}

In the next section, you will learn how to use interceptors to handle web requests-response, before and after the requests are handled by the controller respectively.

主站蜘蛛池模板: 青岛市| 南宁市| 黑龙江省| 丁青县| 台南市| 牙克石市| 漠河县| 青龙| 洮南市| 化德县| 保山市| 三台县| 晋江市| 兴和县| 枞阳县| 张家界市| 柘荣县| 旺苍县| 桦甸市| 饶阳县| 湘潭市| 敦煌市| 三明市| 舟曲县| 乌兰察布市| 土默特左旗| 略阳县| 榆中县| 高清| 和田市| 牡丹江市| 松阳县| 建始县| 通海县| 宣武区| 汽车| 闸北区| 东光县| 襄汾县| 阿克陶县| 河曲县|