springmvc教程--RESTful支持详解
生活随笔
收集整理的這篇文章主要介紹了
springmvc教程--RESTful支持详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?RESTful支持
1.1?需求
RESTful方式商品修改、商品查詢。
1.2?添加DispatcherServlet的rest配置
<servlet><servlet-name>springmvc-servlet-rest</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc-servlet-rest</servlet-name><url-pattern>/</url-pattern></servlet-mapping>1.3?URL?模板模式映射
@RequestMapping(value="/?editItem/{item_id}"):{×××}占位符,請求的URL可以是“/editItem/1”或“/editItem/2”,通過在方法中使用@PathVariable獲取{×××}中的×××變量。
@RequestMapping("/ editItem/{item_id}") public String useredit(@PathVariable("item_id ") String id,Model model) throws Exception{//方法中使用@PathVariable獲取useried的值,使用model傳回頁面model.addAttribute("userid", userid);return"/user/useredit";}如果RequestMapping中表示為"/?editItem/{id}",id和形參名稱一致,@PathVariable不用指定名稱。
?
商品查詢的controller方法也改為rest實現:
// 查詢商品列表@RequestMapping("/queryItem")public ModelAndView queryItem() throws Exception {// 商品列表List<Items> itemsList = itemService.findItemsList(null);// 創建modelAndView準備填充數據、設置視圖ModelAndView modelAndView = new ModelAndView();// 填充數據modelAndView.addObject("itemsList", itemsList);// 視圖modelAndView.setViewName("item/itemsList");return modelAndView;}1.4?靜態資源訪問<mvc:resources>
spring mvc 的<mvc:resources mapping="" location="">實現對靜態資源進行映射訪問。
如下是對js文件訪問配置:
<mvc:resources location="/js/" mapping="/js/**"/>
總結
以上是生活随笔為你收集整理的springmvc教程--RESTful支持详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springmvc教程(4)
- 下一篇: 蓝桥杯-c++_ch04_02_修正版(