• 2----------RestFul和控制器


    控制器Controller

    • 控制器复杂提供访问应用程序的行为,通常通过接口定义或注解定义两种方法实现。

    • 控制器负责解析用户的请求并将其转换为一个模型。

    • 在Spring MVC中一个控制器类可以包含多个方法

    • 在Spring MVC中,对于Controller的配置方式有很多种

    实现Controller接口

    使用注解

    RestFul风格

    概念

    RestFul就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更加简洁,更有层次,更易于实现缓存等机制。

    功能

    资源:互联网所有的事物都可以呗抽象为资源

    资源定位:使用POST、DELETE、PUT、GET,使用不同的方法对资源进行操作。
    分别对应添加、删除、修改、查询。

    简单来说:就是用地址和请求方式,区分提交的服务。

     根据参数类型的不同

    package com.sicheng.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/HelloController")
    public class HelloController {
    
        @RequestMapping("/hello1/{p1}/{p2}")
        public String hello1 (@PathVariable int p1, @PathVariable int p2, Model model){
            int result = p1 + p2;
            model.addAttribute("msg", result);
            return "test1";
        }
    
        @RequestMapping("/hello2/{p1}/{p2}")
        public String hello2 (@PathVariable int p1, @PathVariable String p2, Model model){
            String result = p1 + p2;
            model.addAttribute("msg", result);
            return "test2";
        }
    }

     

     

     根据参数个数的不同

    xxxx应该可以的吧,自己实现

    根据提交方式的不同

    package com.sicheng.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    @RequestMapping("/HelloController")
    public class HelloController {
    
        @RequestMapping(value = "/hello1/{p1}/{p2}", method = RequestMethod.POST)
        public String hello1 (@PathVariable int p1, @PathVariable int p2, Model model){
            int result = p1 + p2;
            model.addAttribute("msg", result);
            return "test1";
        }
    
        @RequestMapping(value = "/hello1/{p1}/{p2}", method = RequestMethod.GET)
        public String hello2 (@PathVariable int p1, @PathVariable int p2, Model model){
            int result = p1 + p2;
            model.addAttribute("msg", result);
            return "test2";
        }
    
    //    @RequestMapping("/hello2/{p1}/{p2}")
    //    public String hello2 (@PathVariable int p1, @PathVariable String p2, Model model){
    //        String result = p1 + p2;
    //        model.addAttribute("msg", result);
    //        return "test2";
    //    }
    }
  • 相关阅读:
    未能加载包“Microsoft SQL Server Data Tools”
    SharePoint 客户端对象模型共用ClientContext的坑
    安装VisualStudio 2015 x64 中文企业版失败
    Could not load file or assembly 'Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0 系统找不到指定的文件。
    为Sharepoint 2010 批量创建SharePoint测试用户
    User Profile Service Application 配置同步连接时,报 MOSS MA not found
    SharePoint 2010 系统账户没完全控制权限了
    百度编辑器 UEditor 报错汇总
    更改SharePoint 2007/2010/2013 Web 应用程序端口号
    SharePoint 2013 报:网站在改进过程中处于只读状态,对此给您带来的不便,我们深表歉意
  • 原文地址:https://www.cnblogs.com/sicheng-li/p/13157895.html
Copyright © 2020-2023  润新知