菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析

一、概述。
       SpringMVC在服务端把客户端传过来的JSON字符串,并把JSON字符串转成 JSON对象并取得其中的属性值,这个在项目中经常用到。
二、代码演示。

需要添加的jar包。


2.1 web.xml。

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://xmlns.jcp.org/xml/ns/javaee"   
  4.     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
  5.     id="WebApp_ID" version="3.1">  
  6. <filter>  
  7.     <filter-name>encodingFilter</filter-name>  
  8.       
  9.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  10.       
  11.     <init-param>  
  12.         <param-name>encoding</param-name>  
  13.         <param-value>utf-8</param-value>  
  14.     </init-param>  
  15. </filter>  
  16. <filter-mapping>  
  17.     <filter-name>encodingFilter</filter-name>  
  18.     <url-pattern>/*</url-pattern>  
  19. </filter-mapping>  
  20.   
  21. <servlet>  
  22.         <servlet-name>springMVC</servlet-name>  
  23.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  24.         <load-on-startup>1</load-on-startup>  
  25.     </servlet>  
  26.     <servlet-mapping>  
  27.         <servlet-name>springMVC</servlet-name>  
  28.         <url-pattern>*.spring</url-pattern>  
  29.     </servlet-mapping>  
  30.     <welcome-file-list>  
  31.         <welcome-file>index.jsp</welcome-file>  
  32.     </welcome-file-list>  
  33. </web-app>  


2.2springMVC-servlet.xml。

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.         http://www.springframework.org/schema/context   
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.           http://www.springframework.org/schema/mvc   
  11.         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">  
  12.       
  13.     <context:component-scan base-package="com.gaowei.JSON" />  
  14.   
  15.     <mvc:annotation-driven />  
  16.       
  17.   
  18. </beans>  


2.3 JSP文件。

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. <script type="text/javascript" src="jquery-1.3.2.js">  
  9. </script>  
  10.     <script type="text/javascript" src="json2.js">  
  11.     </script>  
  12.     <script type="text/javascript">  
  13.         function userinfo(username,password){  
  14.             this.username=username;  
  15.             this.password=password;  
  16.             }  
  17.         function sendAjax(){  
  18.             var userinfoRef=new userinfo('高玮','12312');  
  19.             var jsonStringRef=JSON.stringify(userinfoRef);  
  20.             $.post("getJSONString.spring?t="+new Date().getTime(),{  
  21.                 jsonString:jsonStringRef  
  22.                 });  
  23.             }  
  24.     </script>  
  25. </head>  
  26. <body>  
  27.     <input type="button" onclick="sendAjax()" value="登录" >  
  28. </body>  
  29. </html>  


2.4GetJSONString.java。

  1. package com.gaowei.JSON;  
  2.   
  3. import net.sf.json.JSONObject;  
  4.   
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestParam;  
  8.   
  9. @Controller  
  10. public class GetJSONString {  
  11.       
  12.     @RequestMapping(value="getJSONString")  
  13.     public String getJSONString(@RequestParam("jsonString") String jsonString){  
  14.         JSONObject object=JSONObject.fromObject(jsonString);  
  15.         System.out.println(object.get("username"));  
  16.         System.out.println(object.get("password"));  
  17.         return "test.jsp";  
  18.     }  
  19. }  


2.5效果图。


三、总结

       页面与Contorller层的交互避免要用到JSON传值到后台,这中方法就可以让我们更好的实现界面层与Contorller的交互。???

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值