008_Restfull请求风格
生活随笔
收集整理的這篇文章主要介紹了
008_Restfull请求风格
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. Restful風格url上的參數(shù)通過{}占位符綁定
2. 占位符參數(shù)名與方法參數(shù)名不一致時, 通過@PathVariable綁定
3. 例子
3.1. 新建一個名為SpringMVCRestfull的Web工程, 拷入相關jar包
3.2. 新建一個Item.java的實體類
package com.lywgames.domain;import java.io.Serializable; import java.util.Date;public class Item implements Serializable{private static final long serialVersionUID = 1L;// 商品idprivate Integer id;// 商品名稱private String name;// 商品價格private Double price;// 商品創(chuàng)建時間private Date createtime;// 商品描述private String detail;public Item() { }public Item(Integer id, String name, Double price, Date createtime, String detail) {this.id = id;this.name = name;this.price = price;this.createtime = createtime;this.detail = detail;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}public Date getCreatetime() {return createtime;}public void setCreatetime(Date createtime) {this.createtime = createtime;}public String getDetail() {return detail;}public void setDetail(String detail) {this.detail = detail;} }3.3. 新建一個ItemAction.java的處理器
package com.lywgames.web.action;import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.lywgames.domain.Item;@Controller @RequestMapping("item") public class ItemAction {List<Item> itemList = new ArrayList<Item>();public ItemAction() {itemList.add(new Item(1, "冰箱", 1999.0, new Date(), "保鮮。"));itemList.add(new Item(2, "電腦", 8888.0, new Date(), "網(wǎng)上沖浪"));itemList.add(new Item(3, "洗衣機", 4000.0, new Date(), "從此不用手。"));itemList.add(new Item(4, "空調(diào)", 2600.0, new Date(), "冬天制熱, 夏天制冷。"));itemList.add(new Item(5, "液晶電視", 20000.0, new Date(), "曲面屏幕"));}@RequestMapping(value="findAllProducts")public ModelAndView findAllProducts() { ModelAndView mav = new ModelAndView();mav.addObject("itemList", itemList);mav.setViewName("itemList");return mav;}// Restful風格url上的參數(shù)通過{}占位符綁定@RequestMapping(value="productDetail/{id}")// 占位符參數(shù)名與方法參數(shù)名不一致時, 通過@PathVariable綁定public ModelAndView productDetail(@PathVariable("id") Integer ids) { ModelAndView mav = new ModelAndView();mav.addObject("item", itemList.get(ids));mav.setViewName("item");return mav;}}3.4. 在src目錄下新建springmvc.xml配置
3.5. 修改web.xml
3.6. 編寫index.jsp
3.7. 編寫itemList.jsp
3.8. 編寫item.jsp
3.9. 運行項目
3.10. 查詢所有商品
3.11. 商品詳情, 請求使用了Restfull風格
總結(jié)
以上是生活随笔為你收集整理的008_Restfull请求风格的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 007_请求返回Json
- 下一篇: 009_拦截器