04_springmvc注解开发

一.注解开发

需求:1、进入商品查询列表页面。

   2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询),要修改的商品从数据库查询,根据商品id(主键)查询商品信息。

   3、在商品修改页面,修改商品信息,修改后,点击提交。

1.mapper开发

这里已经有逆向工程生成对应的mapper.xml和mapper.java

2.service开发

功能:商品查询、根据id查询商品信息、根据id修改商品信息、根据id删除商品信息、

package com.ssm.service;

import com.ssm.po.ItemsCustom;
import com.ssm.po.ItemsQueryVo;

import java.util.List;

/**
 * Description:ItemsService
 * User: jiatp
 * Date:2019/9/7 0007 下午 5:22
*/

public interface ItemsService {
    //商品查询
    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;

    //根据id查询商品信息
    public ItemsCustom findItemsById(int id)throws Exception;

    //根据id修改商品信息
    public void updateItems(Integer id,ItemsCustom itemsCustom)throws Exception;
    //根据id删除商品信息
    public void deleteItems(Integer[] ids)throws Exception;


}

3.开发controller

功能:修改页面的展示、修改页面的提交、

package com.ssm.controller;

import com.ssm.exception.CustomException;
import com.ssm.po.ItemsCustom;
import com.ssm.po.ItemsQueryVo;
import com.ssm.service.ItemsService;
import com.ssm.validation.ValidGroup1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * Description:
 * User: jiatp
 * Date:2019/9/7 0007 下午 8:40
*/

@Controller
@RequestMapping("/items")
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    //商品信息查询
    @RequestMapping("/queryItems")
    public ModelAndView queryItems(HttpServletRequest request, ItemsQueryVo itemsQueryVo) throws Exception{
        String id = request.getParameter("id");
        System.out.println(id);
        //调用service查找数据库,查询商品列表,这里使用静态模拟
        List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo);

        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("itemsList",itemsList);//相当于request.setAtttributes
        modelAndView.setViewName("items/itemsList");//指定返回的视图
        return modelAndView;
    }

    /**
     * @Author jiatp
     * @Date 2019/9/8 0008 下午 12:21
     * @MethodName
     * @MethodParameter
     * @MethodReturnType
     * @declare 商品修改页展示
    */
//    @RequestMapping(value = "/editItems",method = {RequestMethod.GET,RequestMethod.POST})
//    public ModelAndView editItems() throws Exception{
//        //调用servie查询 根据id
//        ItemsCustom itemsCustom = itemsService.findItemsById(1);
//
//        //定义ModelAndView
//        ModelAndView  modelAndView = new ModelAndView();
//        //将商品信息放在modelandView
//        modelAndView.addObject("itemsCustom",itemsCustom);
//        //页面展示
//        modelAndView.setViewName("items/editItems");
//        return modelAndView;
//    }
    // controller方法的返回值 这里演示String
/*
    @RequestParam 指定request传入参数和形参进行绑定
    required = true 指定参数是否需要传入
    defaultvalue 可以设置默认值如果id参数没有传入,将默认值和形参进行绑定
*/
    @RequestMapping(value = "/editItems",method = {RequestMethod.GET,RequestMethod.POST})
    public String editItems(Model model, @RequestParam(value = "id",required = true) Integer item_id) throws Exception{
        //调用servie查询 根据id
        ItemsCustom itemsCustom = itemsService.findItemsById(item_id);

        model.addAttribute("itemsCustom",itemsCustom);
        return "items/editItems";
    }


    //商品信息页的修改提交、
    //  在校验pojo之前添加 @Validated,校验之后添加BindingResult接受校验出错的参数
    // 这两个配对出现 顺序是固定的
    @RequestMapping("/editItemsSubmit")
    public String editItemsSubmit(Model model, HttpServletRequest request, Integer id,ItemsCustom itemsCustom) throws Exception{
        //其它操作

        //调用servie更新页面信息
        itemsService.updateItems(id,itemsCustom);
        //重定向 return "forward:queryItems.action"
        return "success";
    }
}

注解@RequestMapping

功能:

  1. url映射:定义controller方法对应的url,进行处理器映射使用。
  2. .窄化请求映射:对url进行分类管理,可以这里定义根路径,最终访问的路径是:根路径+子路径

     3..限制http请求方法:出于安全性考虑,对http的链接进行方法限制。如果限制请求为post方法,进行get请求,报错:

可以这样定义:

controller方法的返回值

1.返回ModelAndView:需要方法结束时,定义ModelAndView,将model和view分别进行设置。

2.返回string:如果controller方法返回string,

 

redirect重定向:redirect重定向特点:浏览器地址栏中的url会变化。修改提交的request数据无法传到重定向的地址。因为重定向后重新进行request(request无法共享)

forward页面转发:通过forward进行页面转发,浏览器地址栏url不变,request可以共享。

3.返回void

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

测试........

原文地址:https://www.cnblogs.com/jatpeo/p/11767522.html