--- tags: Spring MVC --- # 查詢單筆資料 想法:若請求為/product?id=xxx,就進行單筆資料查詢 並且可以設定type與price為「非必須」參數 Controller中寫下列方法 ``` @GetMapping("/product") //注意 是product 不是products public String getProductById( @RequestParam("id") Integer id, @RequestParam(value = "type",required = false,defaultValue = "沒有type資料") String type, @RequestParam(value = "price",required = false,defaultValue = "0.0") Double price, Model model ){ model.addAttribute("product", productService.getProductById(id));//注意 是product 不是products return "product"; } ``` @RequestParam(value = "type",required = false,defaultValue = "沒有type資料") String type required代表是否為必需之參數,若不寫為true defaultValue 代表預設值
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up