夜间福利网站,免费动漫av,一级做a爰片久久毛片免费陪,夜夜骑首页,黄色毛片视频,插插插操操操,综合av色

SpringMVC教程之json交互使用

時(shí)間:2025-09-25 04:30:10 java語(yǔ)言

SpringMVC教程之json交互使用

  Spring MVC屬于SpringFrameWork的后續(xù)產(chǎn)品,已經(jīng)融合在Spring Web Flow里面。本文是百分網(wǎng)小編搜索整理的關(guān)于SpringMVC教程之json交互使用詳解,供參考學(xué)習(xí),希望對(duì)大家有所幫助!想了解更多相關(guān)信息請(qǐng)持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  json數(shù)據(jù)交互

  1.1 @RequestBody

  作用:@RequestBody注解用于讀取http請(qǐng)求的內(nèi)容(字符串),通過(guò)springmvc提供的HttpMessageConverter接口將讀到的內(nèi)容轉(zhuǎn)換為json、xml等格式的數(shù)據(jù)并綁定到controller方法的參數(shù)上。

  本例子應(yīng)用:@RequestBody注解實(shí)現(xiàn)接收http請(qǐng)求的json數(shù)據(jù),將json數(shù)據(jù)轉(zhuǎn)換為Java對(duì)象

  1.2 @ResponseBody

  作用:該注解用于將Controller的方法返回的對(duì)象,通過(guò)HttpMessageConverter接口轉(zhuǎn)換為指定格式的數(shù)據(jù)如:json,xml等,通過(guò)Response響應(yīng)給客戶端

  本例子應(yīng)用:@ResponseBody注解實(shí)現(xiàn)將controller方法返回對(duì)象轉(zhuǎn)換為json響應(yīng)給客戶端

  1.3 請(qǐng)求json,響應(yīng)json實(shí)現(xiàn):

  1.3.1 環(huán)境準(zhǔn)備

  Springmvc默認(rèn)用MappingJacksonHttpMessageConverter對(duì)json數(shù)據(jù)進(jìn)行轉(zhuǎn)換,需要加入jackson的包,如下:

  1.3.2 配置json轉(zhuǎn)換器

  在注解適配器中加入messageConverters

  <!--注解適配器 -->

  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

  <property name="messageConverters">

  <list>

  <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>

  </list>

  </property>

  </bean>

  注意:如果使用<mvc:annotation-driven /> 則不用定義上邊的內(nèi)容。

  1.3.3 controller編寫

  /pic/p>

  @RequestMapping("/editItemSubmit_RequestJson")

  public @ResponseBody Items editItemSubmit_RequestJson(@RequestBody Items items) throws Exception {

  System.out.println(items);

  /pic/p>

  return items;

  }

  1.3.4 頁(yè)面js方法編寫:

  引入 js:

  <script type="text/JavaScript"

  src="${pageContext.request.contextPath }/js/jQuery-1.4.4.min.js"></script>

  /pic/p>

  function request_json(){

  $.ajax({

  type:"post",

  url:"${pageContext.request.contextPath }/item/editItemSubmit_RequestJson.action",

  contentType:"application/json;charset=utf-8",

  data:'{"name":"測(cè)試商品","price":99.9}',

  success:function(data){

  alert(data);

  }

  });

  }

  1.4 Form提交,響應(yīng)json實(shí)現(xiàn):

  采用form提交是最常用的作法,通常有post和get兩種方法,響應(yīng)json數(shù)據(jù)是為了方便客戶端處理,實(shí)現(xiàn)如下:

  1.4.1 環(huán)境準(zhǔn)備

  同第一個(gè)例子

  1.4.2 controller編寫

  /pic/p>

  @RequestMapping("/editItemSubmit_ResponseJson")

  public @ResponseBody Items editItemSubmit_ResponseJson(Items items) throws Exception {

  System.out.println(items);

  /pic/p>

  return items;

  }

  1.4.3 頁(yè)面js方法編寫:

  function formsubmit(){

  var user = " name=測(cè)試商品&price=99.9";

  alert(user);

  $.ajax(

  {

  type:'post',/pic/p>

  url:'${pageContext.request.contextPath}/item/ editItemSubmit_RequestJson.action',

  /pic/x-www-form-urlencoded

  data:user,

  success:function(data){

  alert(data.name);

  }

  }

  )

  }

  從上邊的js代碼看出,已去掉ContentType的定義,ContentType默認(rèn)為:application/x-www-form-urlencoded格式。

  1.4.4 jquery的form插件插件

  針對(duì)上邊第二種方法,可以使用jquery的form插件提交form表單,實(shí)現(xiàn)ajax提交form表單,如下:

  引用js:

  <script type="text/javascript"

  src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>

  <script type="text/javascript"

  src="${pageContext.request.contextPath }/js/jquery.form.min.js"></script>

  js方法如下:

  function response_json() {

  /pic/p>

  var formObj = $("#itemForm");

  /pic/p>

  formObj.ajaxSubmit({

  dataType : "json",/pic/p>

  success : function(responseText) {

  alert(responseText);

  }

  });

  }

  1.4.5 小結(jié)

  實(shí)際開(kāi)發(fā)中常用第二種方法,請(qǐng)求key/value數(shù)據(jù),響應(yīng)json結(jié)果,方便客戶端對(duì)結(jié)果進(jìn)行解析。

【SpringMVC教程之json交互使用】相關(guān)文章:

如何使用Jackson解析JSON示例01-31

在PHP語(yǔ)言中使用JSON12-11

php使用curl發(fā)送json格式數(shù)據(jù)實(shí)例11-25

php中序列化和json使用方法02-21

PS基礎(chǔ)教程之:筆刷的簡(jiǎn)單使用02-08

java中的JSON操作09-13

PHP中Json應(yīng)用03-01

javascript解析json實(shí)例07-24

springmvc如何設(shè)置多視圖器呢02-21