Restful 是一種web軟件架構(gòu)風(fēng)格赌朋,它不是標(biāo)準(zhǔn)也不是協(xié)議图筹,它倡導(dǎo)的是一個(gè)資源定位及資源操作的風(fēng)格。Restful風(fēng)格的url分別對(duì)應(yīng)了GET菲饼、POST肾砂、PUT、DELETE 四種請(qǐng)求宏悦。一般使用時(shí)主要還是使用GET和POST請(qǐng)求镐确,PUT和DELETE基本不會(huì)使用。
GET:一般用于查詢饼煞,獲取資源源葫;
POST:一般用于增加,新建資源砖瞧;
PUT:一般用于更新息堂,修改資源;
DELETE:一般用于刪除資源块促。
Restful風(fēng)格的url中可以包含請(qǐng)求參數(shù)變量荣堰,SpringMVC的Handle可以使用@PathVariable注解從url中獲取參數(shù)變量。
事例代碼:
前端頁(yè)面無(wú)法直接發(fā)送PUT和DELETE請(qǐng)求竭翠,如果想要使用PUT和DELETE請(qǐng)求振坚,需要使用POST請(qǐng)求,并在請(qǐng)求中攜帶_method參數(shù)斋扰,在 _method參數(shù)中指定當(dāng)前請(qǐng)求的方式是PUT還是DELETE渡八。同時(shí)還需要在web.xml中配置請(qǐng)求方式過(guò)濾器,在請(qǐng)求方式過(guò)濾器中解析出_method參數(shù)传货,并將請(qǐng)求轉(zhuǎn)換成對(duì)應(yīng)的請(qǐng)求方式屎鳍。
- 前端 jsp 頁(yè)面代碼
<div>
<h2>SpringMVC對(duì)Restful風(fēng)格url的支持</h2>
<fieldset>
<p>測(cè)試用例:SpringMVC對(duì)Restful風(fēng)格url的支持</p>
<a href="/demo/handle/15">rest_get測(cè)試</a>
<form method="post" action="/demo/handle">
<input type="text" name="username"/>
<input type="submit" value="提交rest_post請(qǐng)求"/>
</form>
<form method="post" action="/demo/handle/15/lisi">
<input type="hidden" name="_method" value="put"/>
<input type="submit" value="提交rest_put請(qǐng)求"/>
</form>
<form method="post" action="/demo/handle/15">
<input type="hidden" name="_method" value="delete"/>
<input type="submit" value="提交rest_delete請(qǐng)求"/>
</form>
</fieldset>
</div>
- 后臺(tái) Handle 代碼
@Controller
@RequestMapping("/demo")
public class DemoController {
@RequestMapping(value = "/handle/{id}", method = {RequestMethod.GET})
public ModelAndView handleGet(@PathVariable("id") int id) {
//服務(wù)器時(shí)間
Date date = new Date();
//返回服務(wù)器時(shí)間到前端頁(yè)面
//封裝了數(shù)據(jù)和頁(yè)面信息的modelAndView
ModelAndView modelAndView = new ModelAndView();
//addObject 其實(shí)是向請(qǐng)求域中request.setAttribute("date",date)
modelAndView.addObject("date", date);
//視圖信息(封裝跳轉(zhuǎn)的頁(yè)面信息)
modelAndView.setViewName("success");
System.out.println("date: " + date);
System.out.println("id: " + id);
System.out.println("get 請(qǐng)求執(zhí)行了......");
return modelAndView;
}
@RequestMapping(value = "/handle", method = {RequestMethod.POST})
public ModelAndView handlePost(String username) {
//服務(wù)器時(shí)間
Date date = new Date();
//返回服務(wù)器時(shí)間到前端頁(yè)面
//封裝了數(shù)據(jù)和頁(yè)面信息的modelAndView
ModelAndView modelAndView = new ModelAndView();
//addObject 其實(shí)是向請(qǐng)求域中request.setAttribute("date",date)
modelAndView.addObject("date", date);
//視圖信息(封裝跳轉(zhuǎn)的頁(yè)面信息)
modelAndView.setViewName("success");
System.out.println("date: " + date);
System.out.println("username: " + username);
System.out.println("post 請(qǐng)求執(zhí)行了......");
return modelAndView;
}
@RequestMapping(value = "/handle/{id}/{username}", method = {RequestMethod.PUT})
public ModelAndView handlePut(@PathVariable("id") int id , @PathVariable("username") String username) {
//服務(wù)器時(shí)間
Date date = new Date();
//返回服務(wù)器時(shí)間到前端頁(yè)面
//封裝了數(shù)據(jù)和頁(yè)面信息的modelAndView
ModelAndView modelAndView = new ModelAndView();
//addObject 其實(shí)是向請(qǐng)求域中request.setAttribute("date",date)
modelAndView.addObject("date", date);
//視圖信息(封裝跳轉(zhuǎn)的頁(yè)面信息)
modelAndView.setViewName("success");
System.out.println("date: " + date);
System.out.println("id: " + id);
System.out.println("username: " + username);
System.out.println("put 請(qǐng)求執(zhí)行了......");
return modelAndView;
}
@RequestMapping(value = "/handle/{id}", method = {RequestMethod.DELETE})
public ModelAndView handleDelete(@PathVariable("id") int id) {
//服務(wù)器時(shí)間
Date date = new Date();
//返回服務(wù)器時(shí)間到前端頁(yè)面
//封裝了數(shù)據(jù)和頁(yè)面信息的modelAndView
ModelAndView modelAndView = new ModelAndView();
//addObject 其實(shí)是向請(qǐng)求域中request.setAttribute("date",date)
modelAndView.addObject("date", date);
//視圖信息(封裝跳轉(zhuǎn)的頁(yè)面信息)
modelAndView.setViewName("success");
System.out.println("date: " + date);
System.out.println("id: " + id);
System.out.println("delete 請(qǐng)求執(zhí)行了......");
return modelAndView;
}
}
- web.xml配置請(qǐng)求方式過(guò)濾器
<!--springmvc提供的針對(duì)post請(qǐng)求的編碼過(guò)濾器-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!--配置springmvc請(qǐng)求方式轉(zhuǎn)換過(guò)濾器,會(huì)檢查請(qǐng)求參數(shù)中是否有_method參數(shù)损离,如果有就 按照指定的請(qǐng)求方式進(jìn)行轉(zhuǎn)換-->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>