【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回
- Ajax:使用JQuery 提供的ajax;==>需要引入jquery.min.js文件;
- Spring MVC;
- Json:提供两种方式返回json数据;



浏览器界面: 


<%@ page language="java" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";response.setHeader("Pragma", "no-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>SpringMVC+ajax+json</title><script type="text/javascript">var basePath = "<%=basePath%>";</script><%-- <link rel="stylesheet" type="text/css" href="<%=basePath%>js/easyui/demo.css"> --%><script type="text/javascript" src="<%=basePath%>js/JQuery/jquery.min.js"></script><script type="text/javascript" src="<%=basePath%>js/test/ajaxAndJson.js"></script></head><body><div style="padding:5px 0;"><p>方式1</p><a href="#" class="easyui-linkbutton" onclick="loadData1()" data-options="iconCls:'icon-add'">ajax异步获取json数据</a></div><div style="margin:10px 0 20px 0;"></div><div style="padding:5px 0;"><p>方式2</p><a href="#" class="easyui-linkbutton" onclick="loadData2()" data-options="iconCls:'icon-add'">ajax异步获取json数据</a></div></body></html>
ajaxAndJson.js文件==>具体的ajax请求


function loadData1() {var actionUrl = basePath + "test/testMVC1.action";var params = {"username" : 'zhangSan','passwd' : '12345678'};$.ajax({url : actionUrl,data : params,dataType : "json",cache : false,error : function(textStatus, errorThrown) {// $.messager.alert('错误', "系统请求错误: " + textStatus, 'error');alert("系统请求错误: " + textStatus);},success : function(data, textStatus) {// $.messager.alert('系统提示', data.username+data.passwd ,'info');alert(data.username+data.passwd );}});}function loadData2() {var actionUrl = basePath + "test/testMVC2.action";var params = {"username" : 'lisi','passwd' : '888888'};$.ajax({url : actionUrl,data : params,dataType : "json",cache : false,error : function(textStatus, errorThrown) {// $.messager.alert('错误', "系统请求错误: " + textStatus, 'error');alert("系统请求错误: " + textStatus);},success : function(data, textStatus) {// $.messager.alert('系统提示', data.Info+", "+data.userList[1].username ,'info');alert(data.Info+", "+data.userList[1].username);}});}
web.xml

<web-app id="WebApp_ID" version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>mvc-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>mvc-dispatcher</servlet-name><url-pattern>/rest/*</url-pattern></servlet-mapping></web-app>



<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 扫描web包,应用Spring的注解 --><context:component-scan base-package="com.ll.web"/><!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面,默认优先级最低 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"p:viewClass="org.springframework.web.servlet.view.JstlView"p:prefix="/jsp/"p:suffix=".jsp" /><!-- bean视图解析器 --><bean class="org.springframework.web.servlet.view.BeanNameViewResolver"p:order="10" /><!-- XMl及JSON视图解析器配置 --><bean id="testMVC"class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"><property name="renderedAttributes"><set><value>userList</value><value>Info</value></set></property></bean><mvc:annotation-driven/></beans>
applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 --><context:component-scan base-package="com.ll.service"/><context:component-scan base-package="com.ll.dao"/></beans>
AjaxController.java


package com.ll.web;import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.ll.model.Person;@Controller@RequestMapping(value = "/test")public class AjaxController {/*** 返回主页* @return*/@RequestMapping(value = "/index.action")public String index() {return "index";}/*** 方式1:* 使用@ResponseBody方式返回json数据* @param username* @param passwd* @return*/@ResponseBody@RequestMapping(value = "/testMVC1.action")public Person testAjax(String username,String passwd) {System.out.println("ajax-使用@ResponseBody方式返回json数据==>" + username + "(" + passwd + ")");return new Person(username,passwd);}/*** 方式2:* 使用MappingJacksonJsonView和bean视图解析器返回json数据* @param mm* @param username* @param passwd* @return*/@RequestMapping(value = "/testMVC2.action")public String getFusionChartsData(ModelMap mm,String username,String passwd) {System.out.println("ajax-使用MappingJacksonJsonView和bean视图解析器返回json数据==>" + username + "(" + passwd + ")");Person p1 = new Person(username+"_1",passwd+"_*1");Person p2 = new Person(username+"_2",passwd+"_*2");List<Person> userList = new ArrayList<Person>();userList.add(p1);userList.add(p2);mm.addAttribute("userList", userList);mm.addAttribute("Info","测试spring MVC");return "testMVC";}}




【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回的更多相关文章
- 【Spring学习笔记-MVC-15.1】Spring MVC之异常处理=404界面
作者:ssslinppp 异常处理请参考前篇博客:<[Spring学习笔记-MVC-15]Spring MVC之异常处理>http://www.cnblogs.com/sssl ...
- 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传
作者:ssslinppp 1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...
- 【Spring学习笔记-MVC-18.1】Spring MVC实现RESTful风格-同一资源,多种展现:xml-json-html
概要 要实现Restful风格,主要有两个方面要讲解,如下: 1. 同一个资源,如果需要返回不同的形式,如:json.xml等: 不推荐的做法: /user/getUserJson /user/get ...
- Spring学习笔记(四)-- Spring事务全面分析
通过本系列的文章对Spring的介绍,我们对Spring的使用和两个核心功能IOC.AOP已经有了初步的了解,结合我个人工作的情况,因为项目是金融系 统.那对事务的控制是不可缺少的.而且是很严格的控制 ...
- Spring学习笔记(一)—— Spring介绍及入门案例
一.Spring概述 1.1 Spring是什么 Spring是一个开源框架,是于2003年兴起的一个轻量级的Java开发框架, 由Rod Johnson 在其著作<Expert one on ...
- Spring学习笔记4—流程(Spring Web Flow)
Spring Web Flow是Spring框架的子项目,作用是让程序按规定流程运行. 1 安装配置Spring Web Flow 虽然Spring Web Flow是Spring框架的子项目,但它并 ...
- Spring学习笔记(二)Spring基础AOP、IOC
Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...
- Spring学习笔记(四)—— Spring中的AOP
一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- spring学习笔记(二)spring中的事件及多线程
我们知道,在实际开发中为了解耦,或者提高用户体验,都会采用到异步的方式.这里举个简单的例子,在用户注册的sh时候,一般我们都会要求手机验证码验证,邮箱验证,而这都依赖于第三方.这种情况下,我们一般会通 ...
随机推荐
- js常用用途
页面元素查找 查找元素方法表达式 返回值 依据元 ...
- L1-042 日期格式化
世界上不同国家有不同的写日期的习惯.比如美国人习惯写成“月-日-年”,而中国人习惯写成“年-月-日”.下面请你写个程序,自动把读入的美国格式的日期改写成中国习惯的日期. 输入格式: 输入在一行中按照“ ...
- WLAN 802.11 a/b/g PHY Specification and EDVT Measurement V
Receive Minimum Input Level (Sensitivity) 测试方法: Receiver Adjacent Channel Rejection (ACR) -For IEEE8 ...
- L232 No methane on Mars
On earth, most of the methane in the atmosphere has been belched by living organisms, so finding the ...
- 阿里云Linux CentOS 7 Docker部署使用gogs搭建自己的git服务器
一.前言 Git是目前优秀和流行的源代码管理工具.而GitHub是一个面向开源及私有软件项目的托管云平台,但开源免费,私有收费.而公司出于商业化等目的需要搭建自己的源代码托管服务器.通过网上了解Gog ...
- Appium 并发测试基于unitest
前言: 在回归测试阶段,UI测试,兼容测试是测试的必要步骤.UI自动化的本身是比较冗余的测试,但是换个角度思考,UI自动化同时连接多台设备,那么在回归测试时,在同一个脚本下产生的测试结果是非常有价值的 ...
- WEB接口测试之Jmeter接口测试自动化 (一)
软件测试自动化从不同的测试阶段分类,可从下层到上层依次分为单元测试-->接口测试-->界面自动化测试. 单元测试一般有开发人员自行完成,而界面自动化测试合适的测试条件又很难达到,测试人员在 ...
- easyui学习笔记10—手风琴格子始终展开和多个格子展开
始终打开有时候可能会很管用,其实就是一个设置问题.这里就不再介绍引用的资源了,这里只看看html是怎么写的. 1.html代码 <body> <h2>Basic Accordi ...
- Windows 7 SP1 多国语言包(MUI language packs)官方下载
为了装一款 Gal Game (为毛不是装 H-Game?),使用 APP 还是太辛苦了,反正相信童鞋们也都用上“瘟妻”了嘛,装个日文的语言包基本上就可以解决问题了.大家都知道只有旗舰版(Wind ...
- openLayers 4 canvas图例绘制,canvas循环添加图片,解决图片闪烁问题
一.问题来源: 接触Openlayers 一段时间了,最近做了一个农业产业系统,项目中涉及到产业图例,最后考虑用canvas来绘制图例图像.当中带图片的图例移动时,图片会实现闪烁留白情况.闪烁是因为绘 ...