1.可以使用servletAPI来实现 ajax

Controller 类

@Controller
public class AjaxController {
@RequestMapping("/ajax.do")
public String ajax(HttpServletResponse resp) throws IOException{
resp.getWriter().print("ajax hello");
return null;
}
}

Jsp

<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function(){
$.post("ajax.do",function(data){
alert(data);
});
});
});
</script>
</head>
<body>
<button id="btn">异步获取数据信息</button>
</body>

2. 使用 springmvc 提供的组件来实现 ajax

导入 jackson 的相关包:

Controller 处理

@RequestMapping("/json.do")
@ResponseBody//将返回内容插入页面中
public List<User> list(){
List<User> list = new ArrayList<User>();
list.add(new User(1,"张三",22));
list.add(new User(2,"李四",32));
return list;
}

配置文件

<!-- json配置 -->
<bean id="stringConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>

Jsp 页面

<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function(){
$.post("json.do",function(data){
var html="";
for(var i=0;i<data.length;i++){
html+="<tr><td>"+data[i].id+"</td><td>"+data[i].name+"</td><td>"+data[i].age+"</td></tr>"
}
$("#content").html(html);
});
});
});
</script>
</head>
<body>
<button id="btn">异步获取数据信息</button>
<table width="80%" align="center">
<tr>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<tbody id="content"></tbody>
</table>
</body>
</html>

配置优化

<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 为响应的视图名称加上前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 为响应的视图名称加上后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<mvc:annotation-driven/>
<context:component-scan base-package="cn.sxt.controller"/>
</beans>

github地址:https://github.com/Vincent-yuan/springmvc_ajax

最新文章

  1. 服务器重启后导致访问ArcServer地图服务须登录
  2. Angularjs 的 ngInfiniteScroll 的使用方法
  3. WebConfig节点详解
  4. vSphere、Hyper-V与XenServer 你选哪个?
  5. The content of the adapter has changed but ListView did not receive a notification
  6. PHP大神的十大优良习惯
  7. nginx,wsgi,flask之间的关系
  8. MarkdownPad2之安装破解
  9. db2备份还原
  10. OAuth2.0学习(1-1)OAuth2.0是什么?
  11. iOS中 图文混排/自定义图文混排 作者:韩俊强
  12. springBoot 使用拦截器 入坑
  13. leetcode 翻转二叉树
  14. 顶点纹理shader
  15. 【CF815D】Karen and Cards 单调栈+扫描线
  16. Dijkstra Java
  17. google网站推广被拒登如何解决
  18. P1546 最短网络(codevs | 2627村村通)
  19. Bootstrap 按钮下拉菜单
  20. vue.js的一些事件绑定和表单数据双向绑定

热门文章

  1. HustOJ二次开发之隐藏菜单栏
  2. [Gamma阶段]第六次Scrum Meeting
  3. JavaScript初探系列(五)——this指向
  4. JAVA字符编码一:Unicode,GBK,GB2312,UTF-8概念基础
  5. Chrome调试工具Developer Tools——前端必备神器
  6. 内存映射文件MappedByteBuffer和Buffer的Scattering与Gathering
  7. MySQL索引原理(一)
  8. SNF快速开发平台2019-角色、权限、账户的概念理解-非常全的理论讲解权限控制
  9. Leetcode: Closest Leaf in a Binary Tree
  10. asp.netcore 高并发下使用HttpClient的方法