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
- < web – app version = “2.5” XMLNS = “java.sun.com/xml/ns/java…
- XMLNS: xsi = “HTTP: / / www.w3.org/2001/XMLSch…
- Xsi: schemaLocation = “java.sun.com/xml/ns/java…
- Java.sun.com/xml/ns/java…
- <servlet>
- <servlet-name>springMVC</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springMVC</servlet-name>
- <url-pattern>*.spring</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
springMVC-servlet.xml
[html] view plain copy
- The < beans XMLNS = “www.springframework.org/schema/bean.
- XMLNS: MVC = “www.springframework.org/schema/mvc”…
- XMLNS: p = “www.springframework.org/schema/p” x…
- xsi:schemaLocation=”
- www.springframework.org/schema/bean…
- www.springframework.org/schema/bean…
- www.springframework.org/schema/cont…
- www.springframework.org/schema/cont…
- www.springframework.org/schema/mvc…
- www.springframework.org/schema/mvc/…
- <context:component-scan base-package=”com.gaowei.JSON” />
- <mvc:annotation-driven />
- </beans>
2. Java code > > > userinfo.java
[java] view plain copy
- package com.gaowei.entity;
- public class Userinfo {
- private String username;
- private String password;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
Test.java
[java] view plain copy
- package com.gaowei.JSON;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import com.gaowei.entity.Userinfo;
- @Controller
- public class Test {
- @RequestMapping(value=”getJSON1″)
- public void getJSON1(@RequestBody Userinfo userinfo){
- System.out.println(“——getJSON1—start—-“);
- System.out.println(userinfo.getUsername());
- System.out.println(userinfo.getPassword());
- System.out.println(“——getJSON1—end—-“);
- }
- @RequestMapping(value=”getJSON2″)
- public void getJSON2(@RequestBody ArrayList<String> list){
- System.out.println(“——getJSON2—start—-“);
- for (int i = 0; i < list.size(); i++) {
- System.out.println(list.get(i));
- }
- System.out.println(“——getJSON2—end—-“);
- }
- @RequestMapping(value=”getJSON3″)
- public void getJSON3(@RequestBody List<Map> list){
- System.out.println(“——getJSON3—start—-“);
- for (int i = 0; i < list.size(); i++) {
- Map map=list.get(i);
- System.out.println(map.get(“username”)+” “+map.get(“password”));
- }
- System.out.println(“——getJSON3—end—-“);
- }
- @RequestMapping(value=”getJSON4″)
- public void getJSON4(@RequestBody Map map){
- System.out.println(“——getJSON4—start—-“);
- System.out.println(map.get(“username”));
- List<Map> workList=(List)map.get(“work”);
- for (int i = 0; i < workList.size(); i++) {
- Map eachAddressMap=workList.get(i);
- System.out.println(“address=”+eachAddressMap.get(“address”));
- }
- Map schoolMap=(Map)map.get(“school”);
- System.out.println(schoolMap.get(“name”));
- System.out.println(schoolMap.get(“address”));
- System.out.println(“——getJSON4—end—-“);
- }
- }
3. Interface code > > > test.jsp
[html] view plain copy
- <%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″ %>
- <html>
- <head>
- < script SRC = “jquery – 1.3.2. Js” >
- </script>
- <script src=”json2.js”>
- </script>
- <script>
- function userinfo(username, password){
- this.username = username;
- this.password = password;
- }
- function sendAjax1(){
- Var userinfoRef = new userinfo(‘ Chinese ‘, ‘Chinese ‘);
- var jsonStringRef = JSON.stringify(userinfoRef);
- $.ajax({
- type: “POST”,
- data: jsonStringRef,
- url: “getJSON1.spring? t=” + new Date().getTime(),
- contentType: “application/json”
- });
- }
- function sendAjax2(){
- var myArray =new Array();
- MyArray on [0] = “1” in China;
- MyArray on [1] = “2” of China;
- MyArray on [2] = “3” of China;
- MyArray on [3] = “4” of China;
- var jsonString=JSON.stringify(myArray);
- $.ajax({
- type: “POST”,
- data: jsonString,
- url: “getJSON2.spring? t=” + new Date().getTime(),
- contentType: “application/json”
- });
- }
- function sendAjax3(){
- var myArray=new Array();
- MyArray [0]= new userinfo(‘中国1’, ‘中国1’);
- MyArray [1]= new userinfo(‘中国2’, ‘中国2’);
- MyArray [2]= new userinfo(‘中国3’, ‘中国3’);
- MyArray [3]= new userinfo(‘中国4’, ‘中国4’);
- var jsonString=JSON.stringify(myArray);
- $.ajax({
- type: “POST”,
- data: jsonString,
- url: “getJSON3.spring? t=” + new Date().getTime(),
- contentType: “application/json”
- });
- }
- function sendAjax4(){
- var jsonObject={
- “username”:”accp”,
- “work”:[{
- “address”:”address1″
- }, {
- “address”:”address2″
- }].
- “school”:{
- “name”:”tc”,
- “address”:”pjy”
- }
- }
- var jsonString=JSON.stringify(jsonObject);
- $.ajax({
- type: “POST”,
- data: jsonString,
- url: “getJSON4.spring? t=” + new Date().getTime(),
- contentType: “application/json”
- });
- }
- </script>
- </head>
- <body>
- <input type=”button” onclick=”sendAjax1()” value=”sendAjax1″/>
- <br/>
- <input type=”button” onclick=”sendAjax2()” value=”sendAjax2″>
- <br/>
- <input type=”button” onclick=”sendAjax3()” value=”sendAjax3″>
- <br/>
- <input type=”button” onclick=”sendAjax4()” value=”sendAjax4″>
- <br/>
- </body>
- </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