Use SpringMVC Json
First of all, after setting up the SpringMVC project environment, if you want to use Spring built-in Json, you need to add an additional 2 JAR package: 1, jackson-core-asl-1.9.9.jar 2, jackson-mapper-asl-1.9.9.jar here I am using Maven, it also corresponds to pox. xml 1 <dependency> 2 <groupId> org. codehaus. jackson </groupId> 3 <artifactId> jackson-core-lgpl </artifactId> 4 <version> 1.8.1 </version> 5 </dependency> 6 7 <dependency> 8 <groupId> org. codehaus. jackson </groupId> 9 <artifactId> jackson-mapper-lgpl </artifactId> 10 <vers Ion> 1.8.1 </version> 11 </dependency> Of course, you can also choose not to use the Json that comes with Spring. There are also many third-party Json support packages, such as Google's Gson, they are all very useful. About the use of Gson, the previous article also has made an introduction, please stamp: "Gson brief use notes" http://www.cnblogs.com/lichenwei/p/3987429.html is good, enter the subject, to use Spring built-in Json, you need to make some configuration in the SpringMVC configuration file: 1 <! -- Process @ RequestMapping annotation at the class level --> 2 <bean 3 class = "org. springframework. web. servlet. mvc. annotation. DefaultAnnotationHandlerMapping"/> 4 <! -- Process @ RequestMapping annotation at the method level --> 5 <bean 6 class = "org. springframework. web. servlet. mvc. annotation. annotationMethodHandlerAdapter "> 7 <property name =" messageConverters "> 8 <list> 9 <bean10 class =" org. springframework. http. converter. stringHttpMessageConverter "> 11 <property name =" supportedMediaTypes "> 12 <list> 13 <! -- The default is ISO-88859-1, avoid garbled here set to UTF-8 --> 14 <value> 15 text/html; charset = UTF-816 </value> 17 </list> 18 </property> 19 </bean> 20 <! -- Start JSON format configuration --> 21 <bean id = "jacksonMessageConverter" 22 class = "org. springframework. http. converter. json. MappingJacksonHttpMessageConverter"> 23 <! -- Solve HttpMediaTypeNotAcceptableException: cocould not find acceptable 24 representation --> 25 <property name = "supportedMediaTypes"> 26 <list> 27 <value> application/json; charset = UTF-8 </value> 28 </list> 29 </property> 30 </bean> 31 </list> 32 </property> 33 </bean> with the above after configuration, we only need to add the annotation @ ResponseBody to the method for processing the AJAX request, so Spring will return the content or object as the body of the HTTP Response and skip the view processing part, the call is suitable for HttpMessageConverter and writes the returned values (objects, strings, List sets, etc.) to the output stream. The usage of SpringMVC is no longer mentioned here. The key code is directly provided: // The Json string @ ResponseBody @ RequestMapping (value = "/showUser", params = "json ", produces = "application/json") public User showUserJson () {User user User = userService. getUserById (1); // get a message from the database return user;} @ RequestMapping value: Specifies the actual address of the request, the specified address can be in URI Template mode (described later); produces: Specifies the returned content type, which is returned only when the (Accept) type in the request header contains the specified type; params: specifies that the request must contain certain parameter values for processing. Take a look at the page output: How is it? It is much simpler to output Json than Struts2, with fewer configurations and wildcard settings. Let's take a look at the combination of AJAX and AJAX. The server code is still as above. Then we write a JSP page to test 1 <% @ page language = "java" contentType = "text/html; charset = UTF-8 "2 pageEncoding =" UTF-8 "%> 3 <% 4 String path = request. getContextPath (); 5 String basePath = request. getScheme () + ": //" 6 + request. getServerName () + ":" + request. getServerPort () 7 + path + "/"; 8%> 9