JSON数据交互是最近火起来的,相比于xml,json更快速,通用。这里就用一个入门案例来学习一下json如何进行数据交互的。
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 项目名称 -->
<display-name>JSONTest</display-name>
<!-- 项目欢迎页,优先级按顺序降低 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化时加载配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!-- 1表示容器启动时立即加载servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.3.xsd">
<!-- 使用包扫描 -->
<context:component-scan
base-package="ltd.ourfamily.controller"></context:component-scan>
<!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<!-- 配置静态资源,不会被拦截 -->
<mvc:resources location="/js/" mapping="/js/**"/>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 设置前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 设置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSON数据交互</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.11.1.min.js">
</script>
<script type="text/javascript">
function testJson() {
var username=$("#username").val();
var password=$("#password").val();
var id=$("#id").val();
$.ajax({
url:"${pageContext.request.contextPath}/testJson",
type:"post",
data:JSON.stringify({id:id,username:username,password:password}),
contentType:"application/json;charset=UTF-8",
dataType:"json",
success:function(data){
if(data!=null){
alert("用户名为:"+data.username+",密码为:"+data.password);
}
}
});
}
</script>
</head>
<body>
<form>
id:<input type="number" name="id" id="id"><br>
用户名:<input type="text" name="username" id="username"><br>
密码:<input type="password" name="password" id="password"><br>
<input type="button" value="点我测试" onclick="testJson()">
</form>
</body>
</html>UserController.java:
package ltd.ourfamily.controller;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import ltd.ourfamily.po.Orders;
import ltd.ourfamily.po.User;
@Controller
public class UserController{
/**
* 接受JSON数据,返回JSON格式的结果
* @param orders
* @return
* @throws Exception
*/
@RequestMapping(value="/testJson")
@ResponseBody
public User handleRequest(@RequestBody User user) throws Exception {
System.out.println(user);
return user;
}
}
可以看出,json交互这里用到了ajax异步请求,使用注解@ResponseBody和@RequestBody获取到JSON类型的数据,并自动映射到User对象中。从而完成了JSON的数据交互。
2. RESTful支持
这里直接上案例吧,感觉restful风格和JSON有些相似,只是用了不同的注解。
在UserController类中新添selectUser方法:
然后新添一个restful.jsp页面用于根据编号来查询用户名:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.11.1.min.js">
</script>
<script type="text/javascript">
function search() {
var id=$("#number").val();
$.ajax({
url:"${pageContext.request.contextPath}/user/"+id,
type:"get",
dataType:"json",
success:function(data){
if(data.username!=null){
alert("用户名为:"+data.username);
}else{
alert("您的输入有误");
}
}
});
}
</script>
</head>
<body>
<form>
编号:<input type="text" name="number" id="number"/>
<input type="button" value="搜索" onclick="search()"/>
</form>
</body>
</html>可以发现,restful风格采用了注解@PathVariable来绑定形参,而JSON则选用@RequestBody来绑定形参。相比于这两种,可以看出restful风格更加简便,但是可读性比较差,在restful的URL中,只传了参数值而没有传递参数名!
版权声明:本文为qq_37820491原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。