欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

小編給大家分享一下SpringMVC環(huán)境下如何實(shí)現(xiàn)的Ajax異步請求JSON格式數(shù)據(jù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、平谷ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的平谷網(wǎng)站制作公司

一 環(huán)境搭建

首先是常規(guī)的spring mvc環(huán)境搭建,不用多說,需要注意的是,這里需要引入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時,返回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>
<!-- 啟動Spring MVC的注解功能,完成請求和注解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,開啟擴(kuò)展名支持,false關(guān)閉支持 -->
<property name="favorPathExtension" value="false" />
<!-- 用于開啟 /userinfo/123?format=json的支持 -->
<property name="favorParameter" value="true" />
<!-- 設(shè)置為true以忽略對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 />
<!-- 啟動自動掃描該包下所有的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):

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

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

二 測試實(shí)例

(1)在WEB-INF/jsp目錄下新建了一個index.jsp文件,包含了簡單的jQuery的ajax請求,請求數(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) {
//請求成功
alert("你好" + response.username + "[" + response.age + "],當(dāng)前時間是:" + response.time + ",歡迎訪問:http://www.zifangsky.cn");
},
error : function(msg) {
alert(msg);
}
});
});
});
</script>
</head>
<body>
<input type="text" id="username"
>
<input type="button" id="sub" value="Go"
>
<br>
</body>
</html>

(2)一個簡單的model類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類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)到頁面
*/
@RequestMapping(value = "/hello.html")
public ModelAndView list() {
ModelAndView view = new ModelAndView("index");
return view;
}
/**
* ajax異步請求, 請求格式是json
*/
@RequestMapping(value = "/hello.json", method = { RequestMethod.POST })
@ResponseBody
public Map<String, String> hello(@RequestBody User user) {
// 返回數(shù)據(jù)的Map集合
Map<String, String> result = new HashMap<String, String>();
Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 返回請求的username
result.put("username", user.getUsername());
// 返回年齡
result.put("age", String.valueOf(user.getAge()));
// 返回當(dāng)前時間
result.put("time", format.format(new Date()));
return result;
}
}

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

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

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

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

(4)效果如下:

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

以上是“SpringMVC環(huán)境下如何實(shí)現(xiàn)的Ajax異步請求JSON格式數(shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享標(biāo)題:SpringMVC環(huán)境下如何實(shí)現(xiàn)的Ajax異步請求JSON格式數(shù)據(jù)
文章出自:http://www.aaarwkj.com/article42/gihcec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、關(guān)鍵詞優(yōu)化軟件開發(fā)、全網(wǎng)營銷推廣虛擬主機(jī)、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
人妻少妇精品视频二区| 蜜臀综合亚洲国产精品| 国产精品国产三级区| 人妻少妇久久中文字幕久久| 欧美亚洲另类麻豆综合在线| 人妻一少妇一区二区三区| 国产精品黑丝美女91| 伦理中文字幕一区二区| 热九九这里只有热九九| 色综合亚洲一区二区小说| 国产欧美一区二区三区久久| 欧美一级午夜欧美午夜视频| 国产精品三级电影网| 国产精品一区2区3区| 日本一区二区三区免费黄视频 | 亚洲欧美精品综合久久99| 欧美一区二区亚洲天堂| 国产乱码免费一区二区三区| 91精品国产综合久久香蕉麻豆| 亚洲国产精品成人久久66| 中文字幕日韩不卡顿一区二区| 欧美日韩一区二区三区久久精品| 青青草网站在线观看视频| 日本一区二区三区免费不卡视频 | 亚洲精品乱码国产妇女毛片| 热久久青草精品欧美一区| 开心久久婷婷综合中文字幕| 亚洲欧美综合一区二区三区| 四虎精品视频在线播放| 人妻精品中文字幕一区二区在线| 亚洲国产色一区二区三区| 热99精品视频在线观看| 人妻中文字幕av资源| 四虎在线观看永久地址| 四季一区二区三区av| 亚洲乱色一区二区三区丝袜| av黄色天堂在线观看| 免费成人自拍偷拍视频| 国产成人亚洲精品专区高清| 亚洲乱码在线中文字幕| 国产亚洲美女在线视频视频|