2016-03-08 191 views
-1

我需要你的帮助。我必须将json对象发送给spring mvc控制器并将其取回并在视图中的字段中进行替换。我的项目是Spring MVC。这对我来说是新的。我发布了Controller和jQuery代码。将json对象传递给控制器​​

setOperator = function(newOperator) { 
 

 
     // alert("operator " + newOperator); 
 
      if (newOperator == '=') { 
 
       // alert("newOperator is = "); 
 
       //AJAX, JSON 
 
       json_result = {"jsn_result" : lastNumber}; 
 
       $.ajax({ 
 
        type: "POST", 
 
        contentType : 'application/json; charset=utf-8', 
 
        dataType : 'json', 
 
        url: "http://localhost:8080/calc", 
 
        data: JSON.stringify(json_result), // Note it is important 
 
        success :function(result) { 
 
         console.log(json_result.jsn_result); 
 
        } 
 
       }); 
 
       equalsPressed = true; 
 
       calculate(); 
 
       return; 
 
      } 
 

 
      if (!equalsPressed) { 
 
       // alert("followed by an operator (+, -, *, /)"); 
 
       calculate(); 
 
      } 
 

 
      equalsPressed = false; 
 
      operator = newOperator; 
 
      operatorSet = true; 
 
      lastNumber = parseFloat(currNumberCtl.val()); 
 
       },

@Controller 
 
@RequestMapping("/") 
 
public class CalculatorController { 
 
\t @RequestMapping(method = RequestMethod.GET) 
 
\t public String printWelcome(ModelMap model) { 
 
\t \t return "calculator"; 
 
\t } 
 
}

+0

欢迎使用stackoverflow!请阅读[如何提问](http://stackoverflow.com/help/how-to-ask),并分享您迄今尝试使用的代码。 – user1859022

+0

请将代码作为文本添加到“代码”部分。 – zx485

+0

我必须在控制器中包含哪些更改? – Maria

回答

0

那么你必须做一些 让我们假设你想传递给控制器​​一个JSON是这样的:

{ 
    "name": "The name", 
    "surname": "The surname" 
} 

而且让我们假设你想回到鉴于这样的JSON:

{ 
    "passedFullName": "The name & The surname" 
} 

你必须: 建立输入JSON输出JSON 车型在接收输入创建能的方法JSON和管理它

我会做到以下几点: 输入模型创建:我想创建一个Java类是这样的:

import java.io.Serializable; 
public class InputJson implements Serializable 
{ 

private static final long serialVersionUID = -9159921073367603471L; 
private String name; 
private String surname; 
public String getName() 
{ 
return name; 
} 
public void setName(String name) 
{ 
this.name = name; 
} 
public String getSurname() 
{ 
return surname; 
} 
public void setSurname(String surname) 
{ 
this.surname = surname; 
} 

} 

然后我会创建这样一个类(输出JSON):

import java.io.Serializable; 

public class OutputJson implements Serializable 
{ 
private static final long serialVersionUID = -1504974498083180855L; 
private String passedFullName; 
public String getPassedFullName() 
{ 
return passedFullName; 
} 
public void setPassedFullName(String passedFullName) 
{ 
this.passedFullName = passedFullName; 
} 

} 

然后在您的控制器我想创建这样的方法:

@RequestMapping(method = { RequestMethod.POST }, value = { "/calc" }) 
public ResponseEntity<OutputJson> handleJson(@RequestBody InputJson input) throws Exception 
{ 
    OutputJson result = new OutputJson(); 
    result. setPassedFullName(input.getName()+" & "+input.getSurname()); 
    return new ResponseEntity<OutputJson>(result, HttpStatus.OK); 
} 

当然,你可以做更复杂的对象...这些只是一个简单的示例 我希望这可以帮助

0

首先,为您的json数据创建Java对象,它可能有助于使用在线生成器:JsonSchema2Pojo 然后改变你的方法declaretion:

@RequestMapping(method = RequestMethod.Post,consumes="application/json") 
public @ResponseBody String printWelcome(@RequestBody YourObject model) { 
    return "calculator"; 
} 

所以基本上牢记HTTP方法(POST),您接受数据(JSON,XML或两者)的格式,在@RequestBody用于指示数据必须从恢复Http主体和@ResponseBody如果您的内容必须是原始或序列化,而不是调用您的视图。

相关问题