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