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

2016-11-30 20:51:56来源:CSDN作者:Crazy_Java1234人点击

第七城市

因为用到了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详细配置信息:

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

controller:

@Controllerpublic 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;    }}

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>
第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台