通过angularjs $ http.get Spring MVC中的HashMap发送 [英] sending HashMap by angularjs $http.get in spring mvc

查看:198
本文介绍了通过angularjs $ http.get Spring MVC中的HashMap发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过发送和检索angularjs HashMap和用SpringMVC中的控制器接收它。我已经成功地发送和接收的列表中,但无法发送HashMap中。
我的code为。

I want to send and retrieve HashMap through angularjs and receive it in springmvc controller. I have successfully send and received List but unable to send HashMap. My code is.

$scope.addskill = function(skills){     
//  $scope.list = [];       
//  $scope.list.push(skills.skillName, skills.expMonth, skills.expYear, skills.experties);          
    var map = {};
    map['name'] = skills.skillName;
    map['month'] = skills.expMonth;
    map['year'] = skills.expYear;
    map['experties'] = skills.experties;

    alert(map['name']);
    var response = $http.get('/JobSearch/user/addskill/?map=' +map);
//  var response = $http.get('/JobSearch/user/addskill/?list=' +$scope.list);
    response.success(function(data, status, headers, config){
        $scope.skills = null;
        $timeout($scope.refreshskill,1000);             
    });             
    response.error(function(data, status, headers, config) {
        alert( "Exception details: " + JSON.stringify({data: data}));
    });     
};

我的MVC控制器是:

My mvc Controller is :

@RequestMapping(value = "/addskill", method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void addStudentSkill(@RequestBody HashMap<String,String> map){

    System.out.println(map.get("name"));
/*      
 *      public void addStudentSkill(@RequestParam("list") List list){
    try{    
        StudentSkills skills = new StudentSkills();
        skills.setSkillName(list[0]);
        skills.setExpMonth(Integer.parseInt(list[1]));
        skills.setExpYear(Integer.parseInt(list[2]));
        skills.setExperties(list[3]);
        skills.setStudent(studentService.getStudent(getStudentName()));
        studentService.addStudentSkill(skills);
    }catch(Exception e){};

*/
}

当我发送和接收名单

评论code工作。我想用关键来获取数据。如果有什么更好的方法请建议。

Commented code works when i send and receive List. I want to use key to retrieve data. If there is any better way please suggest.

该错误是不能转换为java.lang.String中的HashMap

The error is cannot convert java.lang.string to hashmap

推荐答案

您要发送的地图作为一个请求参数。而你想在请求主体读它。这不可能工作。 GET请求没有身体无妨。

You're sending the map as a request parameter. And you're trying to read it in the request body. That can't possibly work. And GET requests don't have a body anyway.

下面是你应该怎么做:

var parameters = {};
parameters.name = skills.skillName;
parameters.month = skills.expMonth;
parameters.year = skills.expYear;
parameters.experties = skills.experties;

var promise = $http.get('/JobSearch/user/addskill', {
    params: parameters
});

在春天控制器:

@RequestMapping(value = "/addskill", method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void addStudentSkill(@RequestParam("name") String name,
                            @RequestParam("name") String month,
                            @RequestParam("name") String year,
                            @RequestParam("name") String experties) {
    ...
}

该方法 addStudentSkill ,而事实上,它不返回任何东西尽管如此,鉴于名字,看来这个方法不用于从数据服务器,而是要在服务器上创建的数据。因此这种方法应该被映射到POST请求,并且该数据应该发送作为身体:

That said, given the name of the method addStudentSkill, and the fact that it doesn't return anything, it seems this method is not used to get data from the server, but instead to create data on the server. So this method should be mapped to a POST request, and the data should be sent as the body:

var data = {};
data.name = skills.skillName;
data.month = skills.expMonth;
data.year = skills.expYear;
data.experties = skills.experties;

var promise = $http.post('/JobSearch/user/addskill', params);

和在控制器:

@RequestMapping(value = "/addskill", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.CREATED)
public void addStudentSkill(@RequestBody Map<String, String> data) {
    ...
}

这篇关于通过angularjs $ http.get Spring MVC中的HashMap发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆