最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)

 更新時(shí)間:2016年05月03日 11:23:25   作者:pangfc  
這篇文章主要介紹了SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下

一 環(huán)境搭建

首先是常規(guī)的spring mvc環(huán)境搭建,不用多說(shuō),需要注意的是,這里需要引入jackson相關(guān)jar包,然后在spring配置文件“springmvc-servlet.xml”中添加json解析相關(guān)配置,我這里的完整代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 避免IE執(zhí)行AJAX時(shí),返回JSON出現(xiàn)下載文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="objectMapper">
<bean class="org.codehaus.jackson.map.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"></constructor-arg>
</bean>
</property>
</bean>
</property>
</bean>
<!-- 啟動(dòng)Spring MVC的注解功能,完成請(qǐng)求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json轉(zhuǎn)換器 -->
</list>
</property>
</bean>
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<!-- true,開(kāi)啟擴(kuò)展名支持,false關(guān)閉支持 -->
<property name="favorPathExtension" value="false" />
<!-- 用于開(kāi)啟 /userinfo/123?format=json的支持 -->
<property name="favorParameter" value="true" />
<!-- 設(shè)置為true以忽略對(duì)Accept Header的支持 -->
<property name="ignoreAcceptHeader" value="false" />
<property name="mediaTypes">
<value>
atom=application/atom+xml
html=text/html
json=application/json
xml=application/xml
*=*/*
</value>
</property>
</bean>
<context:annotation-config />
<!-- 啟動(dòng)自動(dòng)掃描該包下所有的Bean(例如@Controller) -->
<context:component-scan base-package="cn.zifangsky.controller" />
<mvc:default-servlet-handler />
<!-- 定義視圖解析器 -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="requestContextAttribute" value="rc" />
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1"></property>
</bean>
</beans>

項(xiàng)目結(jié)構(gòu):

注:我這里測(cè)試使用的完整jar包:http://pan.baidu.com/s/1dEUwdmL

二 測(cè)試實(shí)例

(1)在WEB-INF/jsp目錄下新建了一個(gè)index.jsp文件,包含了簡(jiǎn)單的jQuery的ajax請(qǐng)求,請(qǐng)求數(shù)據(jù)的格式是JSON,具體代碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<script type="text/javascript" src="scripts/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery/jquery.i18n.properties-min-1.0.9.js"></script>
<script type="text/javascript" src="scripts/jquery/jquery.autocomplete.js"></script>
<script type="text/javascript" src="scripts/jquery/jquery.loadmask.js"></script>
<script type="text/javascript" src="scripts/jquery/jquery.form.js"></script>
<script type="text/javascript" src="scripts/jquery/jquery.timers.js"></script>
<title>jQuery i18n</title>
<script type="text/javascript">
$().ready(
function() {
$("#sub").click(
function() {
var name = $("#username").val();
var age = 18;
var user = {"username":name,"age":age};
$.ajax({
url : 'hello.json',
type : 'POST',
data : JSON.stringify(user), // Request body 
contentType : 'application/json; charset=utf-8',
dataType : 'json',
success : function(response) {
//請(qǐng)求成功
alert("你好" + response.username + "[" + response.age + "],當(dāng)前時(shí)間是:" + response.time + ",歡迎訪問(wèn):http://www.zifangsky.cn");
},
error : function(msg) {
alert(msg);
}
});
});
});
</script>
</head>
<body>
<input type="text" id="username"
style="width: 100px; height: 30px; font-size: 20px; font-weight: bold;">
<input type="button" id="sub" value="Go"
style="height: 40px; height: 30px;">
<br>
</body>
</html>

(2)一個(gè)簡(jiǎn)單的model類(lèi)User,代碼如下:

package cn.zifangsky.controller;
public class User {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

(3)controller類(lèi)TestController.java:

package cn.zifangsky.controller;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
@Scope("prototype")
public class TestController {
/**
* 轉(zhuǎn)到頁(yè)面
*/
@RequestMapping(value = "/hello.html")
public ModelAndView list() {
ModelAndView view = new ModelAndView("index");
return view;
}
/**
* ajax異步請(qǐng)求, 請(qǐng)求格式是json
*/
@RequestMapping(value = "/hello.json", method = { RequestMethod.POST })
@ResponseBody
public Map<String, String> hello(@RequestBody User user) {
// 返回?cái)?shù)據(jù)的Map集合
Map<String, String> result = new HashMap<String, String>();
Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 返回請(qǐng)求的username
result.put("username", user.getUsername());
// 返回年齡
result.put("age", String.valueOf(user.getAge()));
// 返回當(dāng)前時(shí)間
result.put("time", format.format(new Date()));
return result;
}
}

關(guān)于具體的執(zhí)行步驟我簡(jiǎn)單說(shuō)一下:

i)項(xiàng)目啟動(dòng)后,在瀏覽器中訪問(wèn):http://localhost:8089/SpringDemo/hello.html,然后會(huì)轉(zhuǎn)到執(zhí)行controller中的list方法,接著會(huì)轉(zhuǎn)到/WEB-INF/jsp/index.jsp(PS:在controller中返回的是邏輯視圖,跟在springmvc-servlet.xml文件中定義的路徑前綴和后綴進(jìn)行拼接后合成文件的真正路徑)

ii)在index.jsp頁(yè)面輸入文字然后點(diǎn)擊按鈕,將會(huì)觸發(fā)ajax請(qǐng)求,這個(gè)請(qǐng)求會(huì)獲取輸入框中的數(shù)據(jù)和默認(rèn)的“age”參數(shù)拼接成json格式字符串最后提交到“hello.json”這個(gè)請(qǐng)求,也就是執(zhí)行controller中的hello方法

iii)hello方法執(zhí)行完畢后會(huì)返回一系列數(shù)據(jù)最后在頁(yè)面中顯示出來(lái)

(4)效果如下:

以上所述是小編給大家介紹的SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)的相關(guān)內(nèi)容,希望對(duì)大家有所幫助!

相關(guān)文章

  • php ajax網(wǎng)站瀏覽統(tǒng)計(jì)功能的簡(jiǎn)單實(shí)現(xiàn)

    php ajax網(wǎng)站瀏覽統(tǒng)計(jì)功能的簡(jiǎn)單實(shí)現(xiàn)

    這個(gè)功能應(yīng)該是很多網(wǎng)站都需要的,這里僅僅實(shí)現(xiàn)了一個(gè)基于文件的簡(jiǎn)易版本,數(shù)據(jù)庫(kù)的版本請(qǐng)自行參考實(shí)現(xiàn),我這里實(shí)現(xiàn)的功能很不完善,比如未過(guò)濾是否為同一訪客,是否為同一IP等等,這里僅僅是給大家提供一個(gè)參考.
    2008-09-09
  • Jquery中ajax提交表單幾種方法(get、post兩種方法)

    Jquery中ajax提交表單幾種方法(get、post兩種方法)

    ajax技術(shù)帶給我們的是良好的用戶體驗(yàn),同時(shí),使用jquery可以簡(jiǎn)化開(kāi)發(fā),提高工作效率,接下來(lái),腳本之家小編給大家分享Jquery中ajax提交表單幾種方法,需要的朋友可以參考下
    2015-09-09
  • AJAX簡(jiǎn)單異步通信實(shí)例分析

    AJAX簡(jiǎn)單異步通信實(shí)例分析

    這篇文章主要介紹了AJAX簡(jiǎn)單異步通信,實(shí)例分析了Ajax異步通信的技巧與相關(guān)注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • js跨域調(diào)用WebService的簡(jiǎn)單實(shí)例

    js跨域調(diào)用WebService的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)?lái)一篇js跨域調(diào)用WebService的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • ajax完美解決的下拉框的onchange問(wèn)題

    ajax完美解決的下拉框的onchange問(wèn)題

    最近老總提了一個(gè)小功能,在搜索網(wǎng)吧列表的時(shí)候加上網(wǎng)吧所屬代理商這個(gè)條件,原有的搜索條件是一個(gè)地區(qū)二級(jí)聯(lián)動(dòng),現(xiàn)在需要根據(jù)不同的地區(qū)顯示不同的代理商集合。
    2010-08-08
  • IE7下ajax之open Method New的說(shuō)明

    IE7下ajax之open Method New的說(shuō)明

    IE7下ajax之open Method New的說(shuō)明...
    2007-06-06
  • jQuery Validator驗(yàn)證Ajax提交表單的方法和Ajax傳參的方法

    jQuery Validator驗(yàn)證Ajax提交表單的方法和Ajax傳參的方法

    這篇文章主要介紹了jQuery Validator驗(yàn)證Ajax提交表單的方法和Ajax傳參的方法,在文中還給大家提到了jquery .ajax提交表單的寫(xiě)法,具體實(shí)例代碼大家參考下本文
    2017-08-08
  • 非常簡(jiǎn)單的Ajax請(qǐng)求實(shí)例附源碼

    非常簡(jiǎn)單的Ajax請(qǐng)求實(shí)例附源碼

    這篇文章為大家推薦了一個(gè)非常簡(jiǎn)單的Ajax請(qǐng)求實(shí)例,可以在不重載頁(yè)面的情況與 Web 服務(wù)器交換數(shù)據(jù),感興趣的小伙伴們可以參考一下
    2015-11-11
  • AJAX 進(jìn)度條實(shí)現(xiàn)代碼

    AJAX 進(jìn)度條實(shí)現(xiàn)代碼

    AJAX 進(jìn)度條實(shí)現(xiàn)代碼,基于java后來(lái),大家可以學(xué)習(xí)下。
    2009-09-09
  • AJAX請(qǐng)求以及解決跨域問(wèn)題詳解

    AJAX請(qǐng)求以及解決跨域問(wèn)題詳解

    最近開(kāi)始學(xué)習(xí)ajax,學(xué)習(xí)ajax必須得掌握的就是跨域請(qǐng)求,實(shí)際上在不同源的地址上發(fā)送請(qǐng)求就是跨域請(qǐng)求,下面這篇文章主要給大家介紹了關(guān)于AJAX請(qǐng)求以及解決跨域問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2022-08-08

最新評(píng)論

泾源县| 石嘴山市| 剑川县| 酒泉市| 吉木萨尔县| 鲁山县| 沈丘县| 荥阳市| 敦煌市| 新河县| 四川省| 土默特左旗| 松滋市| 美姑县| 昌吉市| 怀仁县| 莆田市| 建阳市| 康乐县| 博野县| 绥棱县| 中卫市| 封开县| 巨鹿县| 江山市| 桃江县| 永平县| 荣昌县| 二连浩特市| 澎湖县| 东阳市| 莆田市| 方山县| 叙永县| 龙口市| 赣榆县| 巴青县| 布拖县| 绥滨县| 曲麻莱县| 灵武市|