1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > SpringMVC环境下实现的Ajax异步请求JSON格式数据

SpringMVC环境下实现的Ajax异步请求JSON格式数据

时间:2018-08-10 05:59:46

相关推荐

SpringMVC环境下实现的Ajax异步请求JSON格式数据

web前端|js教程

javascript,SpringMVC,Ajax

web前端-js教程一 环境搭建

婚纱电子相册源码,下载ubuntu内核文件,爬虫设置根目录,jeakins php,ftp做seolzw

首先是常规的spring mvc环境搭建,不用多说,需要注意的是,这里需要引入jackson相关jar包,然后在spring配置文件“springmvc-servlet.xml”中添加json解析相关配置,我这里的完整代码如下:

ocr识别源码,ubuntu 链接被拒绝,tomcat项目怎么打分支,爬虫插件 chrome,学习php开发教程,seo公司就上乐云seolzw

text/html;charset=UTF-8application/json;charset=UTF-8atom=application/atom+xmlhtml=text/htmljson=application/jsonxml=application/xml*=*/*

项目结构:

在线主播网站源码,ubuntu里设置grup,项目在tomcat找不到,scrapy批量爬虫,网站维护php面试,重庆seo_seo小海lzw

二 测试实例

(1)在WEB-INF/jsp目录下新建了一个index.jsp文件,包含了简单的jQuery的ajax请求,请求数据的格式是JSON,具体代码如下:

<base href="">jQuery i18n$().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 + "],当前时间是:" + response.time + ",欢迎访问:");},error : function(msg) {alert(msg);}});});});

(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 {/*** 转到页面*/@RequestMapping(value = "/hello.html")public ModelAndView list() {ModelAndView view = new ModelAndView("index");return view;}/*** ajax异步请求, 请求格式是json*/@RequestMapping(value = "/hello.json", method = { RequestMethod.POST })@ResponseBodypublic Map hello(@RequestBody User user) {// 返回数据的Map集合Map result = new HashMap();Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 返回请求的usernameresult.put("username", user.getUsername());// 返回年龄result.put("age", String.valueOf(user.getAge()));// 返回当前时间result.put("time", format.format(new Date()));return result;}}

关于具体的执行步骤我简单说一下:

i)项目启动后,在浏览器中访问:http://localhost:8089/SpringDemo/hello.html,然后会转到执行controller中的list方法,接着会转到/WEB-INF/jsp/index.jsp(PS:在controller中返回的是逻辑视图,跟在springmvc-servlet.xml文件中定义的路径前缀和后缀进行拼接后合成文件的真正路径)

ii)在index.jsp页面输入文字然后点击按钮,将会触发ajax请求,这个请求会获取输入框中的数据和默认的“age”参数拼接成json格式字符串最后提交到“hello.json”这个请求,也就是执行controller中的hello方法

iii)hello方法执行完毕后会返回一系列数据最后在页面中显示出来

(4)效果如下:

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

浅谈ajax请求技术

Ajax加载菊花loding效果

非常实用的ajax用户注册模块

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。