My jsonp 整合 spring (不太灵活,限制大)

前端之家收集整理的这篇文章主要介绍了My jsonp 整合 spring (不太灵活,限制大)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 
 
package com.xiuye.callbackmapper;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Component;

import com.tienon.bean.Test;
import com.tienon.mapper.TestMapper;
import com.xiuye.jsonp.callback.AbstractJsonpCallback;

@Component
public class JsonpCallbackDemo extends AbstractJsonpCallback {//必须继承AbstractJsonpCallback

	@Resource
	private TestMapper testDao;
	
	public String test1(){
		return "{msg : 'json no parameter'}";
	}
	/**
	 * 返回必须是json字符串
	 * @param map
	 * @return
	 */
	public String test2(Map<String,String[]> map){
		System.out.println("jsonp parameter := " + map);
		return "{msg : 'json have parameter'}";
		
	}
	
	public String test3(){
		
		List<Test> list = this.testDao.findAll();
		JSONArray object = JSONArray.fromObject(list);
		
		return object.toString();
		
	}

}
package com.xiuye.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiuye.jsonp.callback.manager.JsonpCallbackManager;
import com.xiuye.jsonp.execute.DefaultExecute;

@Controller
public class JsonpCallbackController {

	private Logger log = Logger.getLogger(JsonpCallbackController.class);

	@RequestMapping("josnpNoParameter.do")
	@ResponseBody
	public String josnpNoParameter(String callback) {
		log.debug("jsonpNoParameter handling ...");
		// System.out.println("execute : = ......");
		DefaultExecute exec = JsonpCallbackManager.defaultExecute();
		// System.out.println("execute end : = ......");
		return exec.jsonp(callback);

	}

	@RequestMapping("josnpWithParameter.do")
	@ResponseBody
	public String josnpWithParameter(String callback,HttpServletRequest req) {
		log.debug("josnpWithParameter handling ...");
		Map<String,String[]> map = req.getParameterMap();

		// System.out.println("map := " + map);
		// System.out.println("execute : = ......");
		DefaultExecute exec = JsonpCallbackManager.defaultExecute();
		// System.out.println("execute end : = ......");

		return exec.jsonp(callback,map);

	}

	/**
	 * just for jquery jsonp
	 * @param callback
	 * @param req
	 * @return
	 */
	@RequestMapping(value="josnp.do",method=RequestMethod.POST,produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public String josnp(String callback,HttpServletRequest req) {

		log.debug("josnp post ...");

		Map<String,String[]> map = req.getParameterMap();

		DefaultExecute exec = JsonpCallbackManager.defaultExecute();
		log.debug("parameters' map := " + map);
		if (map != null && !map.isEmpty() && map.containsKey("callback")) {
			if (map.size() == 2) {
				return exec.jsonp(callback);
			} else if (map.size() > 2) {
				return exec.jsonp(callback,map);
			}
		}
		return "callback('not have jsonp callback function!')";

	}
	
	@RequestMapping(value="josnp.do",method=RequestMethod.GET,produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public String josnpGet(String callback,HttpServletRequest req) {
		log.debug("jsonp get");
		return this.josnp(callback,req);
	}
	@RequestMapping(value="josnp.do",method=RequestMethod.PUT,produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public String josnpPut(String callback,HttpServletRequest req) {
		log.debug("jsonp put");
		return this.josnp(callback,method=RequestMethod.DELETE,produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public String josnpDelete(String callback,HttpServletRequest req) {
		log.debug("jsonp delete");
		return this.josnp(callback,req);
	}
	
	
}


<beans
	xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"

	xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">




	
	
	<!-- <bean class="com.tienon.callbackmapper.JsonpCallbackDemo"></bean> -->
	<context:component-scan base-package="com.xiuye.callbackmapper"></context:component-scan><!--让mapper生效,但是不能懒加载,否则错误,即不能使用
lazy-init属性-->

</beans>

spring入口配置(web.xml):

servlet>
		<servlet-name>springDispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- it's just for springDispatcher -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/*.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springDispatcher</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>


其他域名的项目请求:
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>APP-JSONP-CALL</title>
<script type="text/javascript" src="js/jquery-2.1.4.js"></script>
<script type="text/javascript" src="http://localhost/js/test.js"></script>
<script type="text/javascript">
	/* function jsonp(s) {
		alert(s);
	} */
	//alert("jsonp no");
	/*不要想无参数后台函数传送参数,否则,后台报错!*/
	$.ajax({
		dataType : "JSONP",jsonp : "callback",//请求自动带上callback参数,callback值为jsonpCallback的值  
		jsonpCallback : "test1",//接口服务器应该返回字符串数据格式:login(JSON数据)  
		type : "POST",url : "http://www.xiuye.com/josnp.do",//接口服务器地址  
		data : {
		/* user : "xiuye" */
		},//请求数据  
		success : function(response) {
			//成功执行  
			console.log(response);
			//alert("jsonp OK");
		},error : function(e) {
			//失败执行  
			alert(e.status + ',' + e.statusText);
		}
	});
	//alert("jsonp have");
	$.ajax({
		dataType : "JSONP",//请求自动带上callback参数,callback值为jsonpCallback的值  
		jsonpCallback : "test2",//接口服务器地址  
		data : {
			user : "xiuye",password : "123456",obj : [ {
				kk : "eee",ll : "dsdf"
			},{
				kk : "123",ll : "56"
			} ]
		},' + e.statusText);
		}
	});
	$.ajax({
		dataType : "JSONP",//请求自动带上callback参数,callback值为jsonpCallback的值  
		jsonpCallback : "test3",//接口服务器地址  
		/* data : {
			
		},//请求数据   */
		success : function(response) {
			//成功执行  
			console.log(response);
			alert("jsonp test3 := " + response);
		},' + e.statusText);
		}
	});
</script>
</head>
<body>

</body>
</html>



注:1.请求的参数个数和方法名必须与后台一致,否则报错.

2.后台接收的参数在Map<String,String[]>中,如有参数只需在mapper类中方法上只能写Map<String,String[]>形参得到,请求参数!

3.限制大(不太灵活方便),也许有隐藏的bug,仅作为练习.


进入 xiuye jsonp jar 下载地址

猜你在找的Json相关文章