日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springmvc学习笔记(10)-springmvc注解开发之商品改动功能

發布時間:2025/3/21 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springmvc学习笔记(10)-springmvc注解开发之商品改动功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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

方法:

  • 商品信息改動頁面顯示
  • 商品信息改動提交
//使用@Controller來標識它是一個控制器 @Controller //為了對url進行分類管理 ,能夠在這里定義根路徑,終于訪問url是根路徑+子路徑 //比方:商品列表:/items/queryItems.action //@RequestMapping("/items") public class ItemsController {@Autowiredprivate ItemsService itemsService;//商品查詢列表@RequestMapping("/queryItems")//實現 對queryItems方法和url進行映射。一個方法相應一個url//一般建議將url和方法寫成一樣public ModelAndView queryItems() throws Exception{//調用service查找數據庫,查詢商品列表List<ItemsCustom> itemsList = itemsService.findItemsList(null);//返回ModelAndViewModelAndView modelAndView = new ModelAndView();//相當于request的setAttribute方法,在jsp頁面中通過itemsList取數據modelAndView.addObject("itemsList",itemsList);//指定視圖//下邊的路徑,假設在視圖解析器中配置jsp的路徑前綴和后綴,改動為items/itemsList//modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");//下邊的路徑配置就能夠不在程序中指定jsp路徑的前綴和后綴modelAndView.setViewName("items/itemsList");return modelAndView;}//商品信息改動頁面顯示@RequestMapping("/editItems")//限制http請求方法,能夠post和get//@RequestMapping(value="/editItems",method={RequestMethod.POST, RequestMethod.GET})public ModelAndView editItems()throws Exception {//調用service依據商品id查詢商品信息ItemsCustom itemsCustom = itemsService.findItemsById(1);// 返回ModelAndViewModelAndView modelAndView = new ModelAndView();//將商品信息放到modelmodelAndView.addObject("itemsCustom", itemsCustom);//商品改動頁面modelAndView.setViewName("items/editItems");return modelAndView;}//商品信息改動提交@RequestMapping("/editItemsSubmit")public ModelAndView editItemsSubmit(HttpServletRequest request, Integer id, ItemsCustom itemsCustom)throws Exception {//調用service更新商品信息,頁面須要將商品信息傳到此方法itemsService.updateItems(id, itemsCustom);//返回ModelAndViewModelAndView modelAndView = new ModelAndView();//返回一個成功頁面modelAndView.setViewName("success");return modelAndView;}}

@RequestMapping

  • url映射

定義controller方法相應的url,進行處理器映射使用。

  • 窄化請求映射
//使用@Controller來標識它是一個控制器 @Controller //為了對url進行分類管理 。能夠在這里定義根路徑,終于訪問url是根路徑+子路徑 //比方:商品列表:/items/queryItems.action @RequestMapping("/items") public class ItemsController {
  • 限制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注解开发之商品改动功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。