- Building Web Apps with Spring 5 and Angular
- Ajitesh Shukla
- 405字
- 2021-07-02 19:38:20
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.
- Mastering Adobe Captivate 2017(Fourth Edition)
- 算法訓練營:入門篇(全彩版)
- Java加密與解密的藝術(第2版)
- 云計算通俗講義(第3版)
- Oracle Exadata專家手冊
- 運用后端技術處理業務邏輯(藍橋杯軟件大賽培訓教材-Java方向)
- 微服務架構深度解析:原理、實踐與進階
- 圖數據庫實戰
- PHP與MySQL權威指南
- SQL Server 入門很輕松(微課超值版)
- Android Game Programming by Example
- Android高級開發實戰:UI、NDK與安全
- Flask開發Web搜索引擎入門與實戰
- Unity 3D UI Essentials
- Mastering Clojure