从controller.js向spring控制器发送数据时获取404

时间:2015-05-26 11:49:29

标签: angularjs spring mongodb

我必须将id发送到spring控制器,但我得到404.我正在使用angularjs,spring和mongodb。我正在获得完美的数据。批准/拒绝按钮ID的onclick正在传递给controller.js,但之后我得到404.

此外,我必须将行索引传递给控制器​​,以便我可以在数据库中更新数据后删除行。这该怎么做?请提供一些逻辑。

HTML

<tbody>
  <tr ng-repeat="task in taskDetails">
    <td style="text-align: center;">{{task.name}}</td>
    <!-- <td style="text-align: center;">{{task.owners}}</td> -->
    <td style="text-align: center;">
      <span ng-repeat="owner in task.owners">{{owner.ownerName.name}}{{$last ? '' : ', '}}</span>
    </td>
    <td  style="text-align:center;">
      <button class="btn btn-mini btn-primary" ng-click="approveTask(task)" value="approveTask">Approve</button>
      <button class="btn btn-mini btn-danger" ng-click="rejectTask(task)" value="rejectTask">Reject</button>
    </td>
  </tr>
</tbody>

控制器

//controller.js
$scope.approveTask = function(task) {
    alert(task);
    var dataObj = {
        id : task.id
    };
    $http.post('/userNotification/task/approve', dataObj).success(function (data) {   
        alert("Approved! "+ data);
    });
}

$scope.rejectTask = function(task) {
    alert(task);
    var dataObj = {
        id : task.id
    };
    $http.post('/userNotification/task/reject'+ dataObj).success(function(data) {
        alert("Rejected! "+ data);
    });
}

Spring Controller

//spring controller

package com.rmtool.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rmtool.mongo.dao.TaskDAO;
import com.rmtool.service.MongoService;

@Controller
@RequestMapping("/userNotification")
public class UserNotificationController {

  @Autowired
  MongoService mongoService;

  @RequestMapping(value = "/fetchTaskForApproval",
                  method =  RequestMethod.GET)
  public @ResponseBody List<TaskDAO> notification(){
    System.out.println("entering notification");
    List<TaskDAO> taskDAOLists=new ArrayList<TaskDAO>();
    taskDAOLists = mongoService.fetchPendingTask("Pending Approval");
    System.out.println("exiting notification");
    System.out.println(taskDAOLists);
    return taskDAOLists;
  }

  @RequestMapping(value = "/task/approve", method = RequestMethod.POST)
  public @ResponseBody void approveTask(@RequestBody TaskDAO task){
    System.out.println("task Id :"+task.getId());
    mongoService.approvePendingTask(task.getId());
  }

  @RequestMapping(value = "/task/reject/{id}", method = RequestMethod.POST)
  public @ResponseBody void rejectTask(@PathVariable("id") String id){
    mongoService.rejectPendingTask(id);
  }
}

1 个答案:

答案 0 :(得分:0)

This is working fine... 
//controller.js

$scope.approveTask = function($index,$task) {
        $scope.currentIndex = $index;
        //alert($task+$scope.currentIndex);
        var dataObj = {
                id : $task.id

            };
                  $http.post('userNotification/approve',dataObj).success(function (data) {   
                  alert("Approved! ");
                  $scope.taskDetails.splice($scope.currentIndex, 1);
                     $scope.currentIndex = -1;
          });
        }

        $scope.rejectTask = function($index,$task) {
            $scope.currentIndex = $index;
            //alert($task+$scope.currentIndex);
            var dataObj = {
                    id : $task.id

                };
          $http.post('userNotification/reject', dataObj).success(function(data) {
             alert("Rejected! ");
             $scope.taskDetails.splice($scope.currentIndex, 1);
             $scope.currentIndex = -1;
          });
        }

//SpringController
@RequestMapping(value = "/approve", method = RequestMethod.POST)
     public @ResponseBody void approveTask(@RequestBody TaskDAO task){
         System.out.println("task Id :"+task.getId());
         mongoService.approvePendingTask(task.getId());
     }


     @RequestMapping(value = "/reject", method = RequestMethod.POST)
     public @ResponseBody void rejectTask(@RequestBody TaskDAO task){
         System.out.println("task Id :"+task.getId());
         mongoService.rejectPendingTask(task.getId());
     }