1.需求:

  从页面中获取记录的id值,对id值对应的这条记录进行显示。

2.我们如果对这条记录进行修改,我们需要先要查询出原有的记录信息。所以在ItemService中添加findItemById()的方法。

  在ItemService接口中添加:

    public Items findItemById(Integer id);

  在ItemServiceImpl实现接口类中添加实现方法:

    @Autowired

    private ItemsMapper itemsMapper;//注入mapper

    @Override
    public Items findItemById(Integer id) {
       Items item=itemsMapper.selectByPrimaryKey(id);
       return item;
    }

3.在控制层的ItemsController中添加获取参数并根据参数进行处理的方法:

/*
     * springmvc默认支持的参数类型,也就是在controller方法中可以加入这些也可以不加
     */
    @RequestMapping("/itemEdit")
    public String itemEdit(HttpServletRequest request
            ,HttpServletResponse response
            ,HttpSession session
            ,Model model){
        //springmvc获取jsp页面的参数
        String idStr=request.getParameter("id");
        //调用业务层,进行按照id查询
        Items item=itemService.findItemById(Integer.parseInt(idStr));
        //将查询结果传到model层,将item信息传到item参数中,供jsp页面进行显示
        //model模型:放入了返回给页面的数据
        /*model底层就是用了request来传递数据。这样model.addAttribute("item", item)就相当于request.addAttribute("item",item)
         * 但是我们还是选择使用model.addAttribute("item", item),因为model对request域进行了扩展。
         */
        model.addAttribute("item", item);//传到model
        //跳转到jsp页面将item中的信息进行展示
        //如果springMVC方法返回一个简单的字符串,那么SpringMvc就会认为这是一个页面的名称(前提是在SpringMvc.xml中将前缀和后缀都已经配置了)
        return "editItem";//跳转到edidItem.jsp页面
        
    }

处理流程是:

  1.springmvc获取jsp页面的参数id

  2.调用业务层,根据获取到的id进行查询

  3.将查询结果传到model层,将item信息传到item参数中,供jsp页面进行显示

  4.跳转到jsp页面进行显示

注意:

  1.springmvc默认支持的参数类型:HttpServletRequest、HttpServletResponse、HttpSession、Model。在controller方法中可以加入这些也可以不加。

  2.model模型:放入了返回给页面的数据。

  model底层就是用了request来传递数据。这样model.addAttribute("item", item)就相当于request.addAttribute("item",item)。但是我们还是选择使model.addAttribute("item", item),因为model对request域进行了扩展。

  3.如果springMVC方法返回一个简单的字符串,那么SpringMvc就会认为这是一个页面的名称(前提是在SpringMvc.xml中将前缀和后缀都已经配置了)

posted on 2018-12-25 21:34  wyhluckydog  阅读(3107)  评论(0编辑  收藏  举报