2016-03-08 192 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如果您的內容必須是原始或序列化,而不是調用您的視圖。

相關問題