SpringMVC教程之json交互使用_第1頁
SpringMVC教程之json交互使用_第2頁
SpringMVC教程之json交互使用_第3頁
SpringMVC教程之json交互使用_第4頁
SpringMVC教程之json交互使用_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

本文格式為Word版,下載可任意編輯——SpringMVC教程之json交互使用SpringMVC教程之json交互使用

SpringMVC屬于SpringFrameWork的后續(xù)產(chǎn)品,已經(jīng)融合在SpringWebFlow里面。本文是我探尋整理的關(guān)于SpringMVC教程之json交互使用詳解,供參考學習,夢想對大家有所扶助!想了解更多相關(guān)信息請持續(xù)關(guān)注我們我!

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

1.1@RequestBody

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

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

1.2@ResponseBody

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

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

1.3苦求json,響應(yīng)json實現(xiàn):

1.3.1環(huán)境打定

Springmvc默認用MappingJacksonHttpMessageConverter對json數(shù)據(jù)舉行轉(zhuǎn)換,需要參與jackson的包,如下:

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

在注解適配器中參與messageConverters

!--注解適配器--

beanclass=org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

propertyname=messageConverters

list

beanclass=org.springframework.http.converter.json.MappingJacksonHttpMessageConverter/bean

/list

/property

/bean

留神:假設(shè)使用mvc:annotation-driven/那么不用定義上邊的內(nèi)容。

1.3.3controller編寫

//商品修改提交json信息,響應(yīng)json信息

@RequestMapping/editItemSubmit_RequestJson

public@ResponseBodyItemseditItemSubmit_RequestJson@RequestBodyItemsitemsthrowsException

System.out.printlnitems;

//itemService.saveItemitems;

returnitems;

1.3.4頁面js方法編寫:

引入js:

scripttype=text/JavaScript

src=$pageContext.request.contextPath/js/jQuery-1.4.4.min.js/script

//苦求json響應(yīng)json

functionrequest_json

$.ajax

type:post,

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

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

data:name:測試商品,price:99.9,

success:functiondata

alertdata;

;

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

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

1.4.1環(huán)境打定

同第一個例子

1.4.2controller編寫

//商品修改提交,提交普遍form表單數(shù)據(jù),響應(yīng)json

@RequestMapping/editItemSubmit_ResponseJson

public@ResponseBodyItemseditItemSubmit_ResponseJsonItemsitemsthrowsException

System.out.printlnitems;

//itemService.saveItemitems;

returnitems;

1.4.3頁面js方法編寫:

functionformsubmit

varuser=name=測試商品price=99.9;

alertuser;

$.ajax

type:post,//這里改為get也可以正常執(zhí)行

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

//ContentType沒指定將默認為:application/x--form-urlencoded

data:user,

success:functiondata

;

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

1.4.4jquery的form插件插件

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

引用js:

scripttype=text/javascript

src=$pageContext.request.contextPath/js/jquery-1.4.4.min.js/script

scripttype=text/javascript

src=$pageContext.request.contextPath/js/jquery.form.min.js/script

js方法如下:

functionresponse_json

//form對象

varformObj=$#itemForm;

//執(zhí)行ajax提交

formObj.ajaxSubmit

dataTyp

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論