zoukankan      html  css  js  c++  java
  • SpringMVC学习--功能完善

    • 简介

      在基本的项目中,无非就是基本的增删改查,前面我们已经实现了一个简单的查询功能,现在来实现增删改功能,来了解实际开发中的运用,以修改功能为例,因为修改功能基本覆盖了增加和删除的运用。

      前面我们实现了查询列表的功能,现在根据查询列表进入到商品详情,然后修改商品信息然后再返回商品列表页面。

    • 修改功能

      1、开发Mapper:根据id查询商品信息、根据id更新Items表的数据

      这个可以使用逆向工程实现,也可以自己实现。

      2、开发service

      service接口:

    1 // 根据id查询商品信息
    2     public ItemsCustom findItemsById(Integer id) throws Exception;
    3     // 修改商品信息
    4     public void updateItems(Integer id, ItemsCustom itemsCustom)
    5             throws Exception;

      service实现类:

     1 public ItemsCustom findItemsById(Integer id) throws Exception {
     2 
     3         Items items = itemsMapper.selectByPrimaryKey(id);
     4         // 中间对商品信息进行业务处理
     5         // ....
     6         // 返回ItemsCustom
     7         ItemsCustom itemsCustom = new ItemsCustom();
     8         // 将items的属性值拷贝到itemsCustom
     9         BeanUtils.copyProperties(items, itemsCustom);
    10 
    11         return itemsCustom;
    12 
    13     }
    14 
    15     @Override
    16     public void updateItems(Integer id, ItemsCustom itemsCustom)
    17             throws Exception {
    18         // 添加业务校验,通常在service接口对关键参数进行校验
    19         // 校验 id是否为空,如果为空抛出异常
    20 
    21         // 更新商品信息使用updateByPrimaryKeyWithBLOBs根据id更新items表中所有字段,包括 大文本类型字段
    22         // updateByPrimaryKeyWithBLOBs要求必须转入id
    23         itemsCustom.setId(id);
    24         itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
    25     }

      3、开发Controller

     1 @Controller
     2 @RequestMapping("/items")
     3 public class ItemController {
     4     @Autowired
     5     private ItemsService itemsService;
     6     @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
     7     //@RequestParam里边指定request传入参数名称和形参进行绑定。
     8     //通过required属性指定参数是否必须要传入
     9     //通过defaultValue可以设置默认值,如果id参数没有传入,将默认值和形参绑定。
    10     public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {    
    11         //调用service根据商品id查询商品信息
    12         ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
    13         //通过形参中的model将model数据传到页面
    14         //相当于modelAndView.addObject方法
    15         model.addAttribute("itemsCustom", itemsCustom);
    16         
    17         return "items/editItems";
    18     }
    19     // 商品信息修改提交
    20     @RequestMapping("/editItemsSubmit")
    21     public String editItemsSubmit(HttpServletRequest request, Integer id,
    22             ItemsCustom itemsCustom) throws Exception {
    23         // 调用service更新商品信息,页面需要将商品信息传到此方法
    24         itemsService.updateItems(id, itemsCustom);
    25         // 重定向到商品查询列表
    26         //return "redirect:queryItems.action";
    27         // 页面转发
    28          return "forward:queryItems.action";
    29         //return "success";
    30     }
    31 }

      从这两个方法中有很多可以总结的:

      1、在类前面加@RequestMapping("/items"),可以窄化请求,是请求根据类的url和方法的url拼接,这样可以按控制器进行分类来实现不同的调用。

      2、在方法前面加@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET}),这样可以对方法的访问进行限制,只要get和post的方法可以访问。

      3、Controller的方法的返回类型有多种,可以是ModelAndView、void或string。

        (1)、返回ModelAndView

              需要方法结束时,定义ModelAndView,将modelview分别进行设置。

        (2)、返回string

            表示返回逻辑视图名:真正视图(jsp路径)=前缀+逻辑视图名+后缀

            redirect重定向:"redirect:queryItems.action"

            forward页面转发:"forward:queryItems.action"

         (3)、返回void:

            在controller方法形参上可以定义requestresponse,使用requestresponse指定响应结果:

            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串");

     

  • 相关阅读:
    Eclipse CDT Linux下内存分析 实战历险
    .Net元编程【Metaprogramming in NET】 序-翻译
    go语言和资料
    代码提交 【转】
    两本有意思的书【代码的未来、淘宝技术这十年】
    C/C++构建系统 GNU autotool
    C/C++构建系统 -工具汇总
    使用Java语言开发微信公众平台(四)——图文消息的发送与响应
    Onsen UI 前端框架(二)
    Maven项目搭建(一):Maven初体验
  • 原文地址:https://www.cnblogs.com/lcngu/p/5510232.html
Copyright © 2011-2022 走看看