當使用URL對應時還可以使用{}占位符號的方式傳遞參數,
package com.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/item") public class ItemController { @RequestMapping("/{itemNo}") public String create(@PathVariable("itemNo") String itemNo) { return "/item/create"; } }在ItemController中,
當您呼叫/webapp/item/123456時,
會執行create()並且幫你把123456綁定到itemNo的變數中,
除了可以使用function的占位符外,
也可以使用class的占位符,
package com.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/product/{productBand}") public class ProductController { @RequestMapping("/{productNo}") public String create(@PathVariable String productBand, @PathVariable String productNo) { return "/Product/create"; } }呼叫/webapp/product/audi/R8,
則會執行create(),
並且綁定productBand=audi,productNo=R8。
沒有留言:
張貼留言