1. 程式人生 > >springMVC正確使用GET POST PUT和DELETE方法,如何傳遞參數

springMVC正確使用GET POST PUT和DELETE方法,如何傳遞參數

組裝 on() sha 直接 一個 rtt gets 參數 refresh

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

springMVC正確使用GET POST PUT和DELETE方法,如何傳遞參數