SpringMVCとjson
1484 ワード
直接インスタンス:
モデル:SpringMVCをパラメータバインドするオブジェクトです
SpringMVCのコントロールの@RequestMapping()
jspページの作成(主にJSの作成):
問題が発生しました.
1.アップロードするデータ型をどのように設定しますか?
2.サーバが返すデータ型をどのように予測しますか?
モデル:SpringMVCをパラメータバインドするオブジェクトです
public class Json {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
SpringMVCのコントロールの@RequestMapping()
@RequestMapping(value="/jsonRequestBody")//@RequestBody
public String json(@RequestBody List<Json> jsons){
for(Json js:jsons){
System.out.println("name:"+js.getName());
}
return "json/page";
}
jspページの作成(主にJSの作成):
<hr>
<input type="button" value=" Json " onclick="sumbitJson()" >
<script type="text/javascript">
function sumbitJson(){
var Ary=[];
var data1={"name":" ","age":22};
var data2={"name":" ","age":23};
Ary.push(data1);
Ary.push(data2);
$.ajax({
url:'<%=request.getContextPath() %>/json/jsonRequestBody',
contentType:"application/json",
data:JSON.stringify(Ary),
type:'post',
dataType:'json',
success:function(d){
alert("success:"+d);
}
});
}
</script>
問題が発生しました.
1.アップロードするデータ型をどのように設定しますか?
2.サーバが返すデータ型をどのように予測しますか?