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的更多相关文章

  1. spring mvc接收ajax提交的JSON数据,并反序列化为对象

    需求:spring mvc接收ajax提交的JSON数据,并反序列化为对象,代码如下: 前台JS代码: //属性要与带转化的对象属性对应 var param={name:'语文',price:16}; ...

  2. 框架-Java:Spring MVC

    ylbtech-框架-Java:Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 We ...

  3. spring mvc实现ajax 分页

    使用到的技术: ·spring 3 mvc ·json ·jquery ·java ·mysql 首先,要了解如何在spring mvc中使用json. 以下主要从Dao和View及Controlle ...

  4. spring mvc 和ajax异步交互完整实例

    Spring MVC 异步交互demo: 1.jsp页面: <%@ page language="java" contentType="text/html; cha ...

  5. 【Java】Spring MVC 扩展和SSM框架整合

    开发web项目通常很多地方需要使用ajax请求来完成相应的功能,比如表单交互或者是复杂的UI设计中数据的传递等等.对于返回结果,我们一般使用JSON对象来表示,那么Spring MVC中如何处理JSO ...

  6. 从零开始学 Java - 搭建 Spring MVC 框架

    没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...

  7. Java框架-Spring MVC理解001

    Spring MVC理解 1.servlet--Spring MVC的本质 2.Spring MVC其实是一个工具,具体的理解可以分为两步:第一步,了解这个工具是怎么创建出来的:第二步,了解这个工具是 ...

  8. Java之Spring mvc详解

    文章大纲 一.Spring mvc介绍二.Spring mvc代码实战三.项目源码下载四.参考文章   一.Spring mvc介绍 1. 什么是springmvc   springmvc是sprin ...

  9. java之spring mvc之helloworld

    这篇主要讲解springmvc的基本的使用,这里以helloworld项目为例. 目录结构: 1. 新建 web 项目 :springmvc_helloworld 2. 在 WebRoot\WEB-I ...

随机推荐

  1. ssh 可以登录但是 sftp 不能登录的解决办法

    将 /etc/ssh/sshd_config 中的 Subsystem      sftp    /usr/libexec/openssh/sftp-server 改为 Subsystem       ...

  2. HDU 6212 Zuma

    Zuma 这个题没有素质!它卡常! 我发现网上很多人的题解都写得很奇怪,也不好确定正确性,所以我借这篇题解表达一下愚见 定义$ dp[i][j][0...4]$表示 0:消完了 1:还剩1个0 2:还 ...

  3. MVVC与乐观锁和悲观锁

    在并发读写数据库时,读操作可能会不一致的数据(脏读).为了避免这种情况,需要实现数据库的并发访问控制,最简单的方式就是加锁访问.由于,加锁会将读写操作串行化,所以不会出现不一致的状态.但是,读操作会被 ...

  4. UE运行sas配置-WIN10

    1.在UE中配置SAS运行的工具: UE--高级---用户工具--工具配置 在命令行输入"D:\soft\SASHome\SASFoundation\9.4\sas.exe" -c ...

  5. SpringBoot——Profile多环境支持

    1.多profile文件形式 主配置文件编写时, 文件名可以是application-{profile}.properties/yml 默认使用的application.properties的配置. ...

  6. exception: TypeError: Cannot read property '_modulesNamespaceMap' of undefined at getModuleByNamespac

    用 Vue.extend 创造的组件构造器和组件,默认是不集成 store 和 router 的. 比如 main.js 中的这个,其实是挂载在根组件 vm 中.并不是注入到全局 Vue 中.所以你用 ...

  7. c# 通过win32 api 得到指定Console application Content

    已知的问题: 1. 调试的时候会报IO 异常,非调试环境是正常的 2. Windows 应用程序才可以使用,可以用非windows应用程序包一层 using System; using System. ...

  8. 爬虫相关-scrapy框架介绍

    性能相关-进程.线程.协程 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢. 串行执行 import requests def fetc ...

  9. Office2019 Word 新建文档豆沙绿背景色失效零时解决方案

    如果只针对Word的话,可以尝试在开发者选项卡中新建一个宏,复制下面的内容进行运行: Sub WritingLayout() ActiveDocument.Background.Fill.Visibl ...

  10. elk使用记录

    1.使用elk查询接口的时候 几个常用参数  http_host.raw 2.具体的接口名称:request_uri 3.想要把左边要查询的显示出来