zoukankan      html  css  js  c++  java
  • springMVC正确使用GET POST PUT和DELETE方法,如何传递参数

    1. 向服务器请求数据:GET

       这是标准的http的GET最擅长的, 应该使用GET请求,但是在使用时候我们会需要传递一个或多个参数给服务器, 

         这些出参数可能是基本数据类型页可能是对象,get方法可以将我们从前台传递的参数直接转换为后台接收的对象,

        但是注意, get最多只能把前台传递的参数解析为一个对象,(既: 跟对象属性一一对应的参数将会被组装成对象),

       不属于的需要单独用@RequestParam接收, 但是也只能接受基本类型的参数,不是接收对象。

    举个栗子:

    js端:

        $scope.pageChange = function() {
            VehicleApplication.get({
                page: $scope.pageInfos.number ? $scope.pageInfos.number - 1 : 0,  // page和size将会被解析成pageabe对象
                size: $scope.pageInfos.size,
                startTime: $scope.query.startTime, // 其他参数需要以@RequestParam接收
                endTime: $scope.query.endTime,
                status: $scope.query.status}, 
                function(response) {
                    $scope.refreshContent(response);
            });
        }

    后台接收:

    	@GetMapping
    	@ResponseBody
    	public Page<VehicleApplicationPageDTO> getStartedApplications(
    			@RequestParam(required = false) String startTime,
    			@RequestParam(required = false) String endTime,
    			@RequestParam(required = false, defaultValue = "ALL") String status,
    			@PageableDefault(page = 0, size = 10, sort = {"id"}, 
    			direction = Sort.Direction.DESC) Pageable pageable){ // 自动组成pageable对象
    
    		...
    	}
    

      

    2. 提交资源到服务器

      用post

    3. 更改服务器资源

      用put

    4. 删除服务器资源

      用delete

  • 相关阅读:
    Travis CI Could not find or load main class org.gradle.wrapper.GradleWrapperMain 错误
    HyperSQL 链接参数中文件的路径
    Maven 在 pom.xml 文件中配置 repositories 仓库
    Flyway Validate failed: Migration checksum mismatch for migration version 1.0.0.01 错误
    Angular 服务
    Angular 主从组件
    Angular 显示英雄列表
    Angular 英雄示例教程
    L2-002. 链表去重---模拟
    HDU2057
  • 原文地址:https://www.cnblogs.com/nelson-hu/p/8556422.html
Copyright © 2011-2022 走看看