2017-04-23 142 views
0

及彼参数为Null是控制器Angularjs弹簧:在控制器

@RequestMapping(value = "/fetchRecord/", method = RequestMethod.POST) 
    @ResponseBody 
    public String fetchRecord(Integer primaryKey) 
    { 
     return this.serviceClass.fetchRecord(primaryKey); 
    } 

代码这里是我的角码

var dataObj = { 
      primaryKey : $scope.primaryKey 
     }; 
     var res = $http.post('/Practice/learn/fetchRecord/', dataObj); 
     res.success(function(data, status, headers, config) { 

      $scope.firstname = data; 
     }); 
     res.error(function(data, status, headers, config) { 

      alert("failure message: " + JSON.stringify({ 
       data : data 
      })); 
     }); 

我能够调试我的代码。虽然我可以在浏览器中检查它的值是否通过了primaryKey。但控制器仍然是空的。

有什么可能的原因吗?

+0

您可能会尝试fetchrecord(@Requestbody Integer primkey) –

+0

面临错误:客户端发送的请求在语法上不正确。 – sparsh610

+0

而$ http.put而不是后 –

回答

0

你应该送一个JSON对象,

试试这个,

var dataObj = { 
      primaryKey : $scope.primaryKey 
}; 
var res = $http.post('/Practice/learn/fetchRecord/', angular.toJson(dataObj)); 
+0

它仍然是空:( – sparsh610

0

您可以从两个方面得到了Controller值:

第一种选择:

分配具有要传递的属性的对象。 假设你有RecordEntity对象,它有一些属性,其中之一就是Integer primaryKey。注释@RequestBody将获得的价值,因此该控制器将是:

后端

@RequestMapping(value = "/fetchRecord/", method = RequestMethod.POST) 
@ResponseBody 
public String fetchRecord(@RequestBody RecordEntity recordEntity) { 
    return "primaryKey from requestBody: " + recordEntity.getPrimaryKey(); 
} 

前端

在前端,你应该发送具有primaryKey属性中的json身体,例如:

http://localhost:8080/Practice/learn/fetchRecord/ 

后身体:

{ 
    "primaryKey": 123 
} 

您控制器将收到的RecordEntity对象的值。


第二个选项:

通过URL传递值,注释@RequestParam将获得价值,因此该控制器将是:

后端

@RequestMapping(value = "/fetchRecord", method = RequestMethod.POST) 
@ResponseBody 
public String fetchRecord(@RequestParam Integer primaryKey) { 
    return "primaryKey from RequestParam: " + primaryKey; 
} 

前端

在URL中你应该?primaryKey发送的值,例如

http://localhost:8080/Practice/learn/fetchRecord?primaryKey=123 

您控制器将收到的Integer primaryKey价值。