zoukankan      html  css  js  c++  java
  • Spring MVC 学习 之

     @Controller:

             在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射。

    首先基本的写法:

     @RestController:作用也是进行对应URL路由映射

    两者区别:就是RestController 可返回数据,Controller只能返回页面,

    简单来说:如果你要 return 返回一个对象或集合 就用RestController  就可以了

    @RequestMapping

             指定URL映射路径,如果在控制器上配置 RequestMapping  ,具体请求方法也配置路径则映射的路径为两者路径的叠加 常用映射如:RequestMapping("/aaa")

            配置映射路径:

    @Controller 注解   这个注解只能跳转页面

    @Controller
    @RequestMapping("/emp")        //如果加在类上,用get的方式访问,会自动匹配get
    public class Manage3Controller {
    
        @RequestMapping(method = RequestMethod.GET)    //get的请求
        public String selectAll(Model model) {
            List<Staff> staffList = managerServiceFromMock.selectAll();
            System.out.println(staffList);
            model.addAttribute("stafflist", staffList);
            return "home";                     //要返回的页面
        }
     //也可以这样写
      @RequestMapping(value ="/emp",method = RequestMethod.GET)
      public String tt(){
       return "home";
      }
    //这样也可以
    @GetMapping("/emp")
    public String input(Book book) {
    return "book_input";
    }

    get方法:接收url带参数

     @RequestMapping("/del/{id}")
        public String del(@PathVariable("id") int id) {
            staffMapper.deleteByPrimaryKey(id);
            return "redirect:/emp1";
        }

    这样也可以:

       @RequestMapping("/del")
        public String del(int id) {
            staffMapper.deleteByPrimaryKey(id);
            return "redirect:/emp1";
        }

    post方法 第一种:

      @RequestMapping(method = RequestMethod.POST)
        public String insert() {return "redirect:/emp1";
        }

     第二种:

     @PostMapping("/book")
        public String submit(@Valid Book book, BindingResult result) {
            if (result.hasErrors())
                return "book_input";
    
            System.out.println("正常业务。。。。");
            return "book_home";
        }
  • 相关阅读:
    栈解旋(unwinding)
    自定义异常
    异常的基本使用
    什么是跨域?
    Vue-Vue-router跳转页面时返回顶部
    jquery&vue backTop缓慢返回顶部的方法应用
    Windows 10下使用U盘安装Ubuntu双系统
    Windows下磁盘分配操作
    在双系统(Windows与Ubuntu)下删除Ubuntu启动项
    .net framework体系结构
  • 原文地址:https://www.cnblogs.com/nongzihong/p/10202328.html
Copyright © 2011-2022 走看看