菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析
2015-11-04 10:45
483 查看
一、概述。
SpringMVC在服务端把客户端传过来的JSON字符串,并把JSON字符串转成 JSON对象并取得其中的属性值,这个在项目中经常用到。
二、代码演示。
需要添加的jar包。
2.1 web.xml。
[html]
view plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
2.2springMVC-servlet.xml。
[html]
view plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="com.gaowei.JSON" />
<mvc:annotation-driven />
</beans>
2.3 JSP文件。
[html]
view plaincopy
<%@ 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="jquery-1.3.2.js">
</script>
<script type="text/javascript" src="json2.js">
</script>
<script type="text/javascript">
function userinfo(username,password){
this.username=username;
this.password=password;
}
function sendAjax(){
var userinfoRef=new userinfo('高玮','12312');
var jsonStringRef=JSON.stringify(userinfoRef);
$.post("getJSONString.spring?t="+new Date().getTime(),{
jsonString:jsonStringRef
});
}
</script>
</head>
<body>
<input type="button" onclick="sendAjax()" value="登录" >
</body>
</html>
2.4GetJSONString.java。
[java]
view plaincopy
package com.gaowei.JSON;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GetJSONString {
@RequestMapping(value="getJSONString")
public String getJSONString(@RequestParam("jsonString") String jsonString){
JSONObject object=JSONObject.fromObject(jsonString);
System.out.println(object.get("username"));
System.out.println(object.get("password"));
return "test.jsp";
}
}
2.5效果图。
三、总结
页面与Contorller层的交互避免要用到JSON传值到后台,这中方法就可以让我们更好的实现界面层与Contorller的交互。???
SpringMVC在服务端把客户端传过来的JSON字符串,并把JSON字符串转成 JSON对象并取得其中的属性值,这个在项目中经常用到。
二、代码演示。
需要添加的jar包。
2.1 web.xml。
[html]
view plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
2.2springMVC-servlet.xml。
[html]
view plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="com.gaowei.JSON" />
<mvc:annotation-driven />
</beans>
2.3 JSP文件。
[html]
view plaincopy
<%@ 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="jquery-1.3.2.js">
</script>
<script type="text/javascript" src="json2.js">
</script>
<script type="text/javascript">
function userinfo(username,password){
this.username=username;
this.password=password;
}
function sendAjax(){
var userinfoRef=new userinfo('高玮','12312');
var jsonStringRef=JSON.stringify(userinfoRef);
$.post("getJSONString.spring?t="+new Date().getTime(),{
jsonString:jsonStringRef
});
}
</script>
</head>
<body>
<input type="button" onclick="sendAjax()" value="登录" >
</body>
</html>
2.4GetJSONString.java。
[java]
view plaincopy
package com.gaowei.JSON;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GetJSONString {
@RequestMapping(value="getJSONString")
public String getJSONString(@RequestParam("jsonString") String jsonString){
JSONObject object=JSONObject.fromObject(jsonString);
System.out.println(object.get("username"));
System.out.println(object.get("password"));
return "test.jsp";
}
}
2.5效果图。
三、总结
页面与Contorller层的交互避免要用到JSON传值到后台,这中方法就可以让我们更好的实现界面层与Contorller的交互。???
相关文章推荐
- Java修饰符 abstract,static,final 的区别详解
- 菜鸟学习Spring——SpringMVC注解版解析不同格式的JSON串 GOOD
- 用java实现反转链表
- 菜鸟学习Spring——SpringMVC注解版将URL中的参数转成实体
- Java语言基础之集合乱舞step1
- Spring缓存注解@Cache使用
- Java POI Excel导出文件名中文乱码
- Java 定时器的使用:每天定时执行任务
- Spring 让 LOB 数据操作变得简单易行
- springmvc注解
- 【Java】【Flume】Flume-NG启动过程源码分析(一)
- SpringMVC第一个例子
- rxJava的使用
- eclipse如何关联javadoc和源码
- Spring的IoC(控制反转) 、DI(依赖注入)
- 运用Eclipse 的Jetty插件直接部署
- 为什么选择 Java 8 ?
- 说说Java中的实例初始化器
- Java 根据年号和第几周得到开始时间和结束时间
- 【Java】【Fulme】Flume-NG源码阅读之SpoolDirectorySource