java之spring mvc之ajax
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
java之spring mvc之ajax的更多相关文章
- spring mvc接收ajax提交的JSON数据,并反序列化为对象
需求:spring mvc接收ajax提交的JSON数据,并反序列化为对象,代码如下: 前台JS代码: //属性要与带转化的对象属性对应 var param={name:'语文',price:16}; ...
- 框架-Java:Spring MVC
ylbtech-框架-Java:Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 We ...
- spring mvc实现ajax 分页
使用到的技术: ·spring 3 mvc ·json ·jquery ·java ·mysql 首先,要了解如何在spring mvc中使用json. 以下主要从Dao和View及Controlle ...
- spring mvc 和ajax异步交互完整实例
Spring MVC 异步交互demo: 1.jsp页面: <%@ page language="java" contentType="text/html; cha ...
- 【Java】Spring MVC 扩展和SSM框架整合
开发web项目通常很多地方需要使用ajax请求来完成相应的功能,比如表单交互或者是复杂的UI设计中数据的传递等等.对于返回结果,我们一般使用JSON对象来表示,那么Spring MVC中如何处理JSO ...
- 从零开始学 Java - 搭建 Spring MVC 框架
没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...
- Java框架-Spring MVC理解001
Spring MVC理解 1.servlet--Spring MVC的本质 2.Spring MVC其实是一个工具,具体的理解可以分为两步:第一步,了解这个工具是怎么创建出来的:第二步,了解这个工具是 ...
- Java之Spring mvc详解
文章大纲 一.Spring mvc介绍二.Spring mvc代码实战三.项目源码下载四.参考文章 一.Spring mvc介绍 1. 什么是springmvc springmvc是sprin ...
- java之spring mvc之helloworld
这篇主要讲解springmvc的基本的使用,这里以helloworld项目为例. 目录结构: 1. 新建 web 项目 :springmvc_helloworld 2. 在 WebRoot\WEB-I ...
随机推荐
- UDF——已知入口压力和流量计算压降
有时候我们在计算内流,比如管道内的流动时,只知道入口压力和流量,而我们想要计算得到出口的压力,这个应该怎么办呢?当然新版本的Fluent已经自带了流量出口边界,而这里我们采用Fluent的UDF来实现 ...
- 五种IO模型
参考文档 https://www.jianshu.com/p/486b0965c296 概念说明 用户空间和内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空 ...
- 从编程哲学到开发应用:Spring的初步应用
一.前言 在学习Spring的过程中,我们学习了很多东西,可是实际应用中,我们究竟用到了那些?常用的又是那种方法呢? 二.书上与开发的差异 1.IOC 1) IOC,控制反转,是Spring框架的核心 ...
- mstar安卓智能电视方案源代码常用修改
优先 替换 Supernova\projects\customerinfo\inc\Customer_Info.h替换 内核中linux/drivers/mmc/core/mmc.c文件 1, key ...
- TermKit的新一代Mac终端,在Ubuntu 11.04 轻松安装TermKit
作为开发人员的必备工具,终端程序却一直没有什么大的变化,TermKit旨在改变这一切,作为下一代的命令行/终端程序,TermKit为我们提供了一个图形化的终端/命令行程序,它可以以可视化的方式展示终端 ...
- Deepin系统中手动开启swap的方法
Deepin系统中手动开启swap的方法 如何设置 swap(交换空间)的大小建议设置和你的实际物理内存一样大,如你的内存是8G的,则可将下面的count的值设为8192(当然这只是参考值,你可根据你 ...
- 部署一个fc网站需要注意的地方
1. php环境 必须5.3 2. yum install nodejs 3. yum install v8-devel 3. 下载v8js php扩展, 版本是 v8js-0.1.3 tar -zx ...
- PHP 简易网页访问统计
传统的网页访问统计,已经有很多,如 51la.百度统计.站长统计 一般都需要引用JS,在你的网页内嵌入JS,这个操作存在风险,并且不可控. 可以考虑使用 [img src.css src.link h ...
- 带有Q_OBJECT的类要放在头文件的第一个类位置,否则可能无法moc
如果头文件中有多个类,带有Q_OBJECT的类要放在头文件的第一个类位置,否则可能无法moc
- 【452】pandas筛选出表中满足另一个表所有条件的数据
参考:pandas筛选出表中满足另一个表所有条件的数据 参考:pandas:匹配两个dataframe 使用 pd.merge 来实现 on 表示查询的 columns,如果都有 id,那么这是很好的 ...