springmvc学习笔记(10)-springmvc注解开发之商品改动功能
springmvc學習筆記(10)-springmvc注解開發之商品改動功能
標簽: springmvc
- springmvc學習筆記10-springmvc注解開發之商品改動功能
- 需求
- 開發mapper
- 開發service
- 開發controller
- RequestMapping
- controller方法的返回值
本文以商品改動為例,記錄springmvc的注解開發。包含mapper,service,controller,@RequestMapping,controller方法的返回值等
需求
操作流程:
- 1.進入商品查詢列表頁面
- 2.點擊改動,進入商品改動頁面,頁面中顯示了要改動的商品。要改動的商品從數據庫查詢,依據商品id(主鍵)查詢商品信息
- 3.在商品改動頁面,改動商品信息,改動后,點擊提交
開發mapper
mapper:
- 依據id查詢商品信息
- 依據id更新Items表的數據
不用開發了,使用逆向project生成的代碼。
開發service
在com.iot.learnssm.firstssm.service.ItemsService中加入兩個接口
//依據id查詢商品信息/**** <p>Title: findItemsById</p>* <p>Description: </p>* @param id 查詢商品的id* @return* @throws Exception*/ItemsCustom findItemsById(Integer id) throws Exception;//改動商品信息/**** <p>Title: updateItems</p>* <p>Description: </p>* @param id 改動商品的id* @param itemsCustom 改動的商品信息* @throws Exception*/void updateItems(Integer id,ItemsCustom itemsCustom) throws Exception;在com.iot.learnssm.firstssm.service.impl.ItemsServiceImpl中實現接口,添加itemsMapper屬性
@Autowired private ItemsMapper itemsMapper;public ItemsCustom findItemsById(Integer id) throws Exception {Items items = itemsMapper.selectByPrimaryKey(id);//中間對商品信息進行業務處理//....//返回ItemsCustomItemsCustom itemsCustom = new ItemsCustom();//將items的屬性值復制到itemsCustomBeanUtils.copyProperties(items, itemsCustom);return itemsCustom; }public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {//加入業務校驗,通常在service接口對關鍵參數進行校驗//校驗 id是否為空,假設為空拋出異常//更新商品信息使用updateByPrimaryKeyWithBLOBs依據id更新items表中全部字段。包含 大文本類型字段//updateByPrimaryKeyWithBLOBs要求必須轉入iditemsCustom.setId(id);itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom); }開發controller
方法:
- 商品信息改動頁面顯示
- 商品信息改動提交
@RequestMapping
- url映射
定義controller方法相應的url,進行處理器映射使用。
- 窄化請求映射
- 限制http請求方法
出于安全性考慮。對http的鏈接進行方法限制。
//商品信息改動頁面顯示//@RequestMapping("/editItems")//限制http請求方法。能夠post和get@RequestMapping(value="/editItems",method={RequestMethod.POST, RequestMethod.GET})public ModelAndView editItems()throws Exception {假設限制請求為post方法。進行get請求,即將上面代碼的注解改為@RequestMapping(value="/editItems",method={RequestMethod.POST})
報錯,狀態碼405:
controller方法的返回值
- 返回ModelAndView
須要方法結束時,定義ModelAndView,將model和view分別進行設置。
- 返回string
假設controller方法返回string
1.表示返回邏輯視圖名。
真正視圖(jsp路徑)=前綴+邏輯視圖名+后綴
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET}) //@RequestParam里邊指定request傳入參數名稱和形參進行綁定。 //通過required屬性指定參數是否必須要傳入 //通過defaultValue能夠設置默認值。假設id參數沒有傳入。將默認值和形參綁定。 //public String editItems(Model model, @RequestParam(value="id",required=true) Integer items_id)throws Exception { public String editItems(Model model)throws Exception { //調用service依據商品id查詢商品信息 ItemsCustom itemsCustom = itemsService.findItemsById(1); //通過形參中的model將model數據傳到頁面 //相當于modelAndView.addObject方法 model.addAttribute("itemsCustom", itemsCustom); return "items/editItems"; }2.redirect重定向
商品改動提交后,重定向到商品查詢列表。
redirect重定向特點:瀏覽器地址欄中的url會變化。改動提交的request數據無法傳到重定向的地址。由于重定向后又一次進行request(request無法共享)
//重定向到商品查詢列表 //return "redirect:queryItems.action";3.forward頁面轉發
通過forward進行頁面轉發,瀏覽器地址欄url不變,request能夠共享。
//頁面轉發 return "forward:queryItems.action";- 返回void
在controller方法形參上能夠定義request和response。使用request或response指定響應結果:
1.使用request轉向頁面,例如以下:
request.getRequestDispatcher("頁面路徑").forward(request, response);
2.也能夠通過response頁面重定向:
response.sendRedirect("url")
3.也能夠通過response指定響應結果。比如響應json數據例如以下:
response.setCharacterEncoding("utf-8"); response.setContentType("application/json;charset=utf-8"); response.getWriter().write("json串");作者@brianway很多其它文章:個人站點 | CSDN | oschina
posted on 2017-07-03 14:15 mthoutai 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/mthoutai/p/7110903.html
總結
以上是生活随笔為你收集整理的springmvc学习笔记(10)-springmvc注解开发之商品改动功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【2017-07-03】JS连续删除ta
- 下一篇: pyhton (一)基础