好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁(yè) > 知識(shí)庫(kù) > SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)

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

熱門(mén)標(biāo)簽:個(gè)人怎樣在百度地圖標(biāo)注地名 ai機(jī)器人電銷(xiāo)資源 越南河內(nèi)地圖標(biāo)注 超級(jí)大富翁地圖標(biāo)注 硅語(yǔ)電話機(jī)器人公司 騰訊地圖標(biāo)注位置能用多久 機(jī)器人電銷(xiāo)騙局揭秘 云呼外撥網(wǎng)絡(luò)電話系統(tǒng) 地圖標(biāo)注項(xiàng)目怎么樣

一 環(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 MapString, String> hello(@RequestBody User user) {
// 返回?cái)?shù)據(jù)的Map集合
MapString, String> result = new HashMapString, 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ì)大家有所幫助!

您可能感興趣的文章:
  • 使用SpringBoot注解方式處理事務(wù)回滾實(shí)現(xiàn)
  • spring boot注解方式使用redis緩存操作示例
  • Spring AOP如何整合redis(注解方式)實(shí)現(xiàn)緩存統(tǒng)一管理詳解
  • Spring 使用注解方式進(jìn)行事務(wù)管理配置方式
  • spring mvc整合freemarker基于注解方式
  • Spring-MVC異步請(qǐng)求之Servlet異步處理
  • springmvc處理異步請(qǐng)求的示例
  • 詳解spring mvc對(duì)異步請(qǐng)求的處理
  • Spring中注解方式的異步請(qǐng)求

標(biāo)簽:林芝 遼源 洛陽(yáng) 內(nèi)蒙古 鄭州 邢臺(tái) 海南 舟山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)》,本文關(guān)鍵詞  SpringMVC,環(huán)境,下,實(shí)現(xiàn),的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 没带罩子被c了一天免费视频 | 学长在宿舍里破了我的处| 欧美性精品videofree| 国内精品久久久久精品综合紧润丹 | 女人让男人桶爽30分钟动态图 | 饥渴人妻脱了奶罩滴奶水| 福利片欧美| xxxxx古代性xxxx| 日日操夜夜骑| 6080国产精品一区二区| 国产亚洲精品久久久| 艳妇臀荡乳欲伦岳动漫H| 无套中出丰满人妻无码99蜜AV| 耽美3p肉文| 肉大捧一进一出免费视频麻花视频| 成在人线无码免费AV高潮水 | 1717she免费精品app最新版| 日本mv与欧美mv的区别| 麻豆国产福利91在线| 好舒服快快点受不了| 国产精品白嫩熟妇BBBBBB| 国产一区二区三区波多野吉衣| 调教系| 在办公室狂cao丝袜老师H文| 亚洲AV秘?无码一区坂井成羽| 日日噜噜夜夜狠狠tv视频免费| 日本无码91| 嘿咻视频在线观看| 久久伊人蜜桃av一区二区| 久久免费精品国产视频| 亚洲人成中文字幕在线观看| 91麻豆精品| 用力再深点里面痒| kTV裸妇荡舞表演| 大尺度不收费的直播app| 精品一区二区三区电影| 日韩 精品 无码 系列 视频| 激动网色视频| 亚洲美女一区| 国产精品99久久久久久精品玩具 | 女同精品电影一区二区三区|