Word-wrap: break-word! Important; “>Blog.csdn.net/gwblue/arti…This blog is going to show you how to use @requestBody to parse these different JSON strings. > > Need to reference the JAR package

1. XML configuration > > > web.xml

[html]  view plain  copy

  1. < web – app version = “2.5” XMLNS = “java.sun.com/xml/ns/java…
  2. XMLNS: xsi = “HTTP: / / www.w3.org/2001/XMLSch…
  3. Xsi: schemaLocation = “java.sun.com/xml/ns/java…
  4.     Java.sun.com/xml/ns/java…
  5.     <servlet>  
  6.         <servlet-name>springMVC</servlet-name>  
  7.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  8.         <load-on-startup>1</load-on-startup>  
  9.     </servlet>  
  10.     <servlet-mapping>  
  11.         <servlet-name>springMVC</servlet-name>  
  12.         <url-pattern>*.spring</url-pattern>  
  13.     </servlet-mapping>  
  14.     <welcome-file-list>  
  15.         <welcome-file>index.jsp</welcome-file>  
  16.     </welcome-file-list>  
  17. </web-app>  

springMVC-servlet.xml

[html]  view plain  copy

  1. The < beans XMLNS = “www.springframework.org/schema/bean.
  2. XMLNS: MVC = “www.springframework.org/schema/mvc”…
  3. XMLNS: p = “www.springframework.org/schema/p” x…
  4.     xsi:schemaLocation=”  
  5.         www.springframework.org/schema/bean…
  6.         www.springframework.org/schema/bean…
  7.         www.springframework.org/schema/cont…
  8.         www.springframework.org/schema/cont…
  9.           www.springframework.org/schema/mvc…
  10.         www.springframework.org/schema/mvc/…
  11.     <context:component-scan base-package=”com.gaowei.JSON” />  
  12.     <mvc:annotation-driven />  
  13.       
  14.   
  15. </beans>  

2. Java code > > > userinfo.java

[java]  view plain  copy

  1. package com.gaowei.entity;  
  2.   
  3. public class Userinfo {  
  4.     private String username;  
  5.     private String password;  
  6.     public String getUsername() {  
  7.         return username;  
  8.     }  
  9.     public void setUsername(String username) {  
  10.         this.username = username;  
  11.     }  
  12.     public String getPassword() {  
  13.         return password;  
  14.     }  
  15.     public void setPassword(String password) {  
  16.         this.password = password;  
  17.     }  
  18. }  

Test.java

[java]  view plain  copy

  1. package com.gaowei.JSON;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestBody;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10.   
  11.   
  12.   
  13.   
  14.   
  15. import com.gaowei.entity.Userinfo;  
  16.   
  17. @Controller  
  18. public class Test {  
  19.       
  20.     @RequestMapping(value=”getJSON1″)  
  21.     public void getJSON1(@RequestBody Userinfo userinfo){  
  22.         System.out.println(“——getJSON1—start—-“);  
  23.         System.out.println(userinfo.getUsername());  
  24.         System.out.println(userinfo.getPassword());  
  25.         System.out.println(“——getJSON1—end—-“);  
  26.     }  
  27.       
  28.     @RequestMapping(value=”getJSON2″)  
  29.     public void getJSON2(@RequestBody ArrayList<String> list){  
  30.         System.out.println(“——getJSON2—start—-“);  
  31.         for (int i = 0; i < list.size(); i++) {  
  32.             System.out.println(list.get(i));  
  33.         }  
  34.         System.out.println(“——getJSON2—end—-“);  
  35.     }  
  36.       
  37.     @RequestMapping(value=”getJSON3″)  
  38.     public void getJSON3(@RequestBody List<Map> list){  
  39.         System.out.println(“——getJSON3—start—-“);  
  40.         for (int i = 0; i < list.size(); i++) {  
  41.             Map map=list.get(i);  
  42.             System.out.println(map.get(“username”)+” “+map.get(“password”));  
  43.         }  
  44.         System.out.println(“——getJSON3—end—-“);  
  45.     }  
  46.       
  47.     @RequestMapping(value=”getJSON4″)  
  48.     public void getJSON4(@RequestBody Map map){  
  49.         System.out.println(“——getJSON4—start—-“);  
  50.         System.out.println(map.get(“username”));  
  51.         List<Map> workList=(List)map.get(“work”);  
  52.         for (int i = 0; i < workList.size(); i++) {  
  53.             Map eachAddressMap=workList.get(i);  
  54.             System.out.println(“address=”+eachAddressMap.get(“address”));  
  55.         }  
  56.         Map schoolMap=(Map)map.get(“school”);  
  57.         System.out.println(schoolMap.get(“name”));  
  58.         System.out.println(schoolMap.get(“address”));  
  59.         System.out.println(“——getJSON4—end—-“);  
  60.     }  
  61. }  

3. Interface code > > > test.jsp

[html]  view plain  copy

  1. <%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″ %>  
  2. <html>  
  3.     <head>  
  4. < script SRC = “jquery – 1.3.2. Js” >
  5.         </script>  
  6.         <script src=”json2.js”>  
  7.         </script>  
  8.         <script>  
  9.             function userinfo(username, password){  
  10.                 this.username = username;  
  11.                 this.password = password;  
  12.             }  
  13.               
  14.             function sendAjax1(){  
  15. Var userinfoRef = new userinfo(‘ Chinese ‘, ‘Chinese ‘);
  16.                 var jsonStringRef = JSON.stringify(userinfoRef);  
  17.                 $.ajax({  
  18.                     type: “POST”,  
  19.                     data: jsonStringRef,  
  20. url: “getJSON1.spring? t=” + new Date().getTime(),
  21.                     contentType: “application/json”  
  22.                 });  
  23.             }  
  24.   
  25.             function sendAjax2(){  
  26.                 var myArray =new Array();  
  27. MyArray on [0] = “1” in China;
  28. MyArray on [1] = “2” of China;
  29. MyArray on [2] = “3” of China;
  30. MyArray on [3] = “4” of China;
  31.                 var jsonString=JSON.stringify(myArray);  
  32.                   $.ajax({  
  33.                         type: “POST”,  
  34.                         data: jsonString,  
  35. url: “getJSON2.spring? t=” + new Date().getTime(),
  36.                         contentType: “application/json”  
  37.                     });  
  38.                 }  
  39.   
  40.   
  41.             function sendAjax3(){  
  42.                     var myArray=new Array();  
  43. MyArray [0]= new userinfo(‘中国1’, ‘中国1’);
  44. MyArray [1]= new userinfo(‘中国2’, ‘中国2’);
  45. MyArray [2]= new userinfo(‘中国3’, ‘中国3’);
  46. MyArray [3]= new userinfo(‘中国4’, ‘中国4’);
  47.                     var jsonString=JSON.stringify(myArray);  
  48.                       $.ajax({  
  49.                             type: “POST”,  
  50.                             data: jsonString,  
  51. url: “getJSON3.spring? t=” + new Date().getTime(),
  52.                             contentType: “application/json”  
  53.                         });  
  54.                 }  
  55.   
  56.             function sendAjax4(){  
  57.                 var jsonObject={  
  58.                     “username”:”accp”,  
  59.                     “work”:[{  
  60.                         “address”:”address1″  
  61. }, {
  62.                         “address”:”address2″      
  63. }].
  64.                         “school”:{  
  65.                             “name”:”tc”,  
  66.                             “address”:”pjy”  
  67.                             }  
  68.                         }  
  69.                 var jsonString=JSON.stringify(jsonObject);  
  70.                   $.ajax({  
  71.                         type: “POST”,  
  72.                         data: jsonString,  
  73. url: “getJSON4.spring? t=” + new Date().getTime(),
  74.                         contentType: “application/json”  
  75.                     });  
  76.             }  
  77.         </script>  
  78.     </head>  
  79.     <body>  
  80.         <input type=”button” onclick=”sendAjax1()” value=”sendAjax1″/>  
  81.         <br/>  
  82.         <input type=”button” onclick=”sendAjax2()” value=”sendAjax2″>  
  83.         <br/>  
  84.         <input type=”button” onclick=”sendAjax3()” value=”sendAjax3″>  
  85.         <br/>  
  86.         <input type=”button” onclick=”sendAjax4()” value=”sendAjax4″>  
  87.         <br/>  
  88.     </body>  
  89. </html>  

4. The rendering

Word-wrap: break-word! Important; “> > > < span style = “max-width: 100%; clear: both; min-height: 1em; The @requestBody method is a powerful way to convert JSON strings into entity classes, arryLists, maps, etc. This approach has greatly improved the efficiency of our developers