springmvc+ajax+json从页面传数据到后台,以及后台传Map集合到ajax解析

欢迎加入技术交流群: Java开发者技术交流

因为用到了json,需要在springmvc的配置文件里面配置json数据的转换器,这个很重要,并且需要引入一下三个jar包:
jackson-core-2.6.6.jar
jackson-databind-2.6.7.jar
jackson-annotations-2.6.0.jar
这是传输json格式必须得jar包

下面就是springmvc.xml详细配置信息:


<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>

    
    <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> 

controller:

@Controller
public class UserController {

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

        return userMap;
    }

}

jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>

<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>

你可能感兴趣的:(web框架)