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

xiaoxiao2021-02-28  8

首先说明一下,传输的数据格式是为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 οnclick="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>

转载请注明原文地址: https://www.6miu.com/read-1999973.html

最新回复(0)