将JSON数据传递给弹簧控制器的方法有多少?
我关注了this tutorial,他们使用以下语法传递数据:
data: "{\"name\":\"hmkcode\",\"id\":2}",
这样可行,但由于我需要使用文本输入从用户检索数据,所以我不知道如何将变量放在该字符串中。
我尝试使用以下语法:
data: "{\"name\":\name\}"
但它返回以下错误:
status:parsererror er:SyntaxError:Unexpected tokken a
我见过其他使用以下语法的网站:
data: {"name":name}
但这给了我同样的错误。
这有效,但我不知道是否是最佳方法。
var json = {"name" : name};
...
data: JSON.stringify(json),
我设法将JSON字符串传递给我的一个控制器,但是我得到了这样的字符串:
{"name": Joe, "lastname": Smith}
有没有办法只在Person对象中获取该信息,或者至少只在字符串中获取Joe而在另一个中获取Smith?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script type="text/javascript">
function doAjaxPost()
{
// get the form values
var name = $('#name').val();
var lastname = $('#lastname').val();
var json = {"name" : name, "lastname" : lastname};
//console.log(json);
$.ajax(
{
type: "POST",
url: "formShow",
data: JSON.stringify(json),
//data: "{\"name\":name}",
//data: {"name":name},
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
beforeSend: function(xhr)
{
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(data)
{
//console.log(data);
console.log(data.name);
//var data = $.parseJSON(JSON.stringify(response));
//alert(data);
alert( "name: "+data.name);
//$('#name').val('');
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
/* error: function (xhr, ajaxOptions, thrownError)
{
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}*/
});
}
</script>
<fieldset>
<legend>Name in view</legend>
Name in view: <input type="text" id="name" name="name">
<br>
Last Name in view: <input type="text" id="lastname" name="lastname">
<br>
Show modify name in view: <input type="text" id="modifyname" name=""modifyname"">
<br>
<input type="button" value="Add Users" onclick="doAjaxPost()">
</fieldset>
<br>
这些是我的控制者:
@RequestMapping(value = "formShow", method = RequestMethod.GET)
public String formularioIncidencia (Model model) {
return "formShow";
}
@RequestMapping(value = "formShow", method = RequestMethod.POST)
public @ResponseBody String getTags(@RequestBody String name)
{
String variableAjax= name;
System.out.println("controller variable is " + variableAjax);
//that prints me this "{name: Joe, lastname: Smith}"
return variableAjax;
}
EDITED **** 这是我的用户类
public class Userimplements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String lastname;
public User(){}
}
我将控制器编辑为以下
@RequestMapping(value = "formShow", method = RequestMethod.GET)
public String formShow(Model model) {
return "formShow";
}
@RequestMapping(value = "formShow", method = RequestMethod.POST)
public @ResponseBody User getTags(@RequestBody final User user, Model model)
{
//what should i do here parse my user to JSON how??
user.setName("name changed");
model.("modifyname", user.getName() );
return User;
}
答案 0 :(得分:4)
您也可以从Ajax传递数据data:'name='+ name+'&lastname='+ lastname,
在控制器端,您可以使用@RequestParam
注释来获取从ajax调用传递的值。
Ajax代码如下所示:
$.ajax({
type: 'POST',
url:'your controller url',
data:'name='+ name+'&lastname='+ lastname,
success: function(msg){
alert('wow' + msg);
}
});
控制器代码:
@RequestMapping(value = "formShow", method = RequestMethod.POST)
public String getTags(@RequestParam("name") String name, RequestParam("lastname") String lastname)
{
System.out.println("name: " + name+ " lastname: "+lastname);
String fullName = name + lastname;
return fullName;
}
希望这对你有所帮助。 干杯:)
答案 1 :(得分:0)
答案 2 :(得分:0)
一些观察将会启发(我希望)。
在JAVA中:最好指定您的&#34;请求&#34;对象和您的响应对象如下:
@RequestMapping(method = RequestMethod.POST, value = "/rest/path/to/action",
consumes = "application/json", produces = "application/json")
@ResponseStatus(value = HttpStatus.OK)
public @ResponseBody
List<?> action(@RequestBody List<?> requestParam) {
List<String> myret = new ArrayList<String>();
for (int i=0; i < requestParam.size() ;i++){
myret.add(requestParam.get(i).toString());
}
}
在这种情况下,我定义了相同的对象&#34; List&#34;作为我的请求和我的回复对象,但它取决于您的定义。如果要表示用户对象,则必须定义用户对象并使用Jackson lib指定所需的字段。这有点棘手,但是一旦你得到它,你就可以传递你想要的任何物体。
然后您可以按照控制器中的定义在AJAX中传递数据。所以在这种情况下会是:
var postData = ["Somedata", "Someotherdata"];
$.ajax(
{
type: "POST",
url: "/rest/path/to/action",
data: postData, // i can do this, because it is defined this way in my controller
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
//etc etc
我希望它有所帮助:)
干杯