json数据交互
json数据格式在接口调用中、html页面中较常用,json格式比较简单,解析还比较方便。
比如:webservice接口,传输json数据.
1、请求json、输出json,要求请求的是json串,所以在前端页面中需要将请求的内容转成json,不太方便。
2、请求key/value、输出json。此方法比较常用。
配置环境:
添加jar包
注意:如果使用<mvc:annotation-driven />不用配置
@Controller
public class JsonTest {
//请求json串(商品信息),输出json(商品信息)
//@RequestBody将请求的商品信息的json串转成itemsCustom对象
//@ResponseBody将itemsCustom转成json输出
@RequestMapping("/requestJson")
public @ResponseBody ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom){
//@ResponseBody将itemsCustom转成json输出
return itemsCustom;
}
//请求key/value,输出json
@RequestMapping("/responseJson")
public @ResponseBody ItemsCustom responseJson(ItemsCustom itemsCustom){
//@ResponseBody将itemsCustom转成json输出
return itemsCustom;
}
}
<%@ 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>json交互测试</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
//请求json,输出是json
function requestJson(){
$.ajax({
type:'post',
url:'${pageContext.request.contextPath }/requestJson.action',
contentType:'application/json;charset=utf-8',
//数据格式是json串,商品信息
data:'{"name":"手机","price":999}',
success:function(data){//返回json结果
alert(data);
}
});
}
//请求key/value,输出是json
function responseJson(){
$.ajax({
type:'post',
url:'${pageContext.request.contextPath }/responseJson.action',
//请求是key/value这里不需要指定contentType,因为默认就 是key/value类型
//contentType:'application/json;charset=utf-8',
//数据格式是json串,商品信息
data:'name=手机&price=999',
success:function(data){//返回json结果
alert(data.name);
}
});
}
</script>
</head>
<body>
<input type="button" onclick="requestJson()" value="请求json,输出是json"/>
<input type="button" onclick="responseJson()" value="请求key/value,输出是json"/>
</body>
</html>
静态资源的访问:
<!-- 静态资源解析 -->
<mvc:resources location="/js/" mapping="/js/**/"></mvc:resources>
拦截器
定义拦截器,实现HandlerInterceptor接口。接口中提供三个方法。
public class HandlerInterceptor1 implements HandlerInterceptor{
//进入handler方法之前实行
//用于身份认证,身份授权
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object
handler)throws Exception {
// TODO Auto-generated method stub
return false;
}
//进入handler方法之后,返回ModelAndView之前执行,
//应用场景:将公用的模型数据(比如菜单导航)在这里传到视图,也可以在这里统一指定视图
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object
handler,ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
//执行handler完成执行此方法
//应用场景:统一异常处理,统一日志 处理
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object
handler, Exception ex)throws Exception {
// TODO Auto-generated method stub
}
在springmvc配置类似全局的拦截器,springmvc框架将配置的类似全局的拦截器注入到每个HandlerMapping中。
<!-- 配置全局拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="cn.itcast.ssm.interceptor.HandlerInterceptor1"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="cn.itcast.ssm.interceptor.HandlerInterceptor2"></bean>
</mvc:interceptor>
</mvc:interceptors>
拦截器应用(实现登陆认证)
1.定义controller方法:
@RequestMapping("/login")
public String login(HttpSession session, String username, String password) throws Exception{
session.setAttribute("username", username);
return "redirect:/items/queryItems.action";
}
//退出
@RequestMapping("/logout")
public String logout(HttpSession session)throws Exception{
session.invalidate();
return "redirect:/items/queryItems.action";
}
2.拦截器的实现:
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object
handler)throws Exception {
String url = request.getRequestURI();
if(url.indexOf("login.action")>=0){
return true;
}
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
if(username!=null){
return true;
}
request.getRequestDispatcher("/WEB-INF/jsp/items/login.jsp").forward(request,response);
return false;
}
3.配置拦截器(springmvc.xml)
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="cn.itcast.ssm.interceptor.HandlerInterceptor2"></bean>
</mvc:interceptor>