RequestMapping注解
作用
用于建立請求URL和處理請求方法之間的對應(yīng)關(guān)系。
屬性
- value:指定請求的實(shí)際地址,可以是一個字符串或者一個字符串列表。
value可以不寫,直接在括號中寫,默認(rèn)就是value值
@RequestMapping(value=“/hello”)
public String hello(){ return…} - method:指定請求的方式,可以是GET、POST、PUT、DELETE等。
@RequestMapping(value=“/hello”,method=RequestMethod.GET/POST/PUT/DELETE)
等效于 :1.@GetMapping(“/hello”);
?????????????? 2.@PostMapping(“/hello”);
???????????????3.@PutMapping(“/hello”);
???????????????4.@DeleteMapping(“/hello”) - params:指定參數(shù),用于匹配請求路徑中的參數(shù)。
- headers:指定請求頭,用于匹配請求頭中的內(nèi)容。
- produces:指定響應(yīng)的媒體類型,用于匹配響應(yīng)內(nèi)容的類型。
- custom:用于指定自定義的匹配規(guī)則。
位置
1.在類上
寫在類上一般是有二級路徑,類似
-
請求 URL的第一級訪問目錄。此處不寫的話,就相當(dāng)于應(yīng)用的根目錄。寫的話需要以/開頭。它出現(xiàn)的目的是為了使我們的 URL 可以按照模塊化管理,例如:
賬戶模塊:
? /account/add
? /account/update
? /account/delete …
訂單模塊:
? /order/add
? /order/update
? /order/delete
紅色的部分就是把RequsetMappding寫在類上,使我們的URL更加精細(xì)。
2.方法上
請求URL的第二級訪問目錄,可以窄化請求路徑
使用
1.窄化路徑
-
使用二級目錄訪問
@Controller @RequestMapping("/account") public class AccountController { @RequestMapping("/findAccount") public ModelAndView findAccount() { ModelAndView mv = new ModelAndView(); mv.addObject("msg", "歡迎你 springmvc"); mv.setViewName("success"); return mv; } }
-
在index.jsp里面定義超鏈接
<a href="/account/findAccount">窄化路徑</a>
2.method屬性示例
-
描述需要使用指定的請求方式來請求該方法
@Controller @RequestMapping("/account") public class AccountController { //指定的請求方式 @RequestMapping(value = "/findAccount1", method = RequestMethod.POST) public ModelAndView findAccount1() { ModelAndView mv = new ModelAndView(); mv.addObject("msg", "歡迎你 springmvc"); mv.setViewName("success"); return mv; } }
-
測試:在index.jsp里使用get方式請求
<a href="/account/findAccount1">請求方式</a>
結(jié)果:
-
我們再換一種請求方式
<form action="account/findAccount1" method="post"> <input type="submit" value="保存賬戶,post 請求"> </form>
結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-802401.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-802401.html
到了這里,關(guān)于Spring MVC學(xué)習(xí)之——RequestMapping注解的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!