springMVC中使用ajax来接收Controller传输的数据demo

本文介绍如何在SpringMVC中实现JSON数据的交互过程,包括必要的依赖库配置、控制器编写及前端页面调用示例。

首先说明一下,传输的数据格式是为json格式的。因为用到了json,需要在springmvc的配置文件里面配置json数据的转换器,这个很重要,并且需要引入一下三个jar包、这是传输json格式必须得jar包: 

jackson-core-2.6.6.jar 
jackson-databind-2.6.7.jar 
jackson-annotations-2.6.0.jar                                                                                                                                                                      jackson-all-1.6.7.jar(非必须)

下载链接:json数据包

jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">

    function myfun(){       
        var mydata={"name":"zhou","age":"23"};      
        $.ajax( {
        url : "ajax.do",
        dataType:"json",
        data : mydata,
        success : function(data) {
            alert(data.age);
        }
      });
    }
</script>
</head>
<body>
    <input onclick="myfun()" type="button" value="click here">
</body>
</html>
controller:

@Controller
public class UserController {

    @RequestMapping(value = "ajax.do")  
    public @ResponseBody Map<String, Object> ajaxTest(String name, String age) {        
        System.out.println(name+"------"+age);
        Map<String, Object> userMap = new HashMap<String, Object>();
        userMap.put("name", "zhou");
        userMap.put("age", "23");
        userMap.put("gender", "男");
        userMap.put("address", "上海");

        return userMap;
    }

}
springmvc.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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-4.2.xsd  
           http://www.springframework.org/schema/tx   
           http://www.springframework.org/schema/tx/spring-tx-4.2.xsd  
           http://www.springframework.org/schema/mvc  
           http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <context:component-scan base-package="com.zmj"></context:component-scan>
    <mvc:annotation-driven />
    <!--视图解析器  -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--json格式数据转换的配置  -->
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
        p:ignoreDefaultModelOnRedirect="true">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
            </list>
        </property>
    </bean>
</beans> 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张小洛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值