大佬教程收集整理的这篇文章主要介绍了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/scheR_361_11845@a/beans" xmlns:xsi="http://www.w3.org/2001/XMLscheR_361_11845@a-instance" xmlns:context="http://www.springframework.org/scheR_361_11845@a/context" xmlns:jdbc="http://www.springframework.org/scheR_361_11845@a/jdbc" xmlns:jee="http://www.springframework.org/scheR_361_11845@a/jee" xmlns:tx="http://www.springframework.org/scheR_361_11845@a/tx" xmlns:aop="http://www.springframework.org/scheR_361_11845@a/aop" xmlns:mvc="http://www.springframework.org/scheR_361_11845@a/mvc" xmlns:util="http://www.springframework.org/scheR_361_11845@a/util" xsi:scheR_361_11845@aLOCATIOn=" http://www.springframework.org/scheR_361_11845@a/beans http://www.springframework.org/scheR_361_11845@a/beans/spring-beans-4.2.xsd http://www.springframework.org/scheR_361_11845@a/context http://www.springframework.org/scheR_361_11845@a/context/spring-context-4.2.xsd http://www.springframework.org/scheR_361_11845@a/aop http://www.springframework.org/scheR_361_11845@a/aop/spring-aop-4.2.xsd http://www.springframework.org/scheR_361_11845@a/tx http://www.springframework.org/scheR_361_11845@a/tx/spring-tx-4.2.xsd http://www.springframework.org/scheR_361_11845@a/jdbc http://www.springframework.org/scheR_361_11845@a/jdbc/spring-jdbc-4.2.xsd http://www.springframework.org/scheR_361_11845@a/jee http://www.springframework.org/scheR_361_11845@a/jee/spring-jee-4.2.xsd http://www.springframework.org/scheR_361_11845@a/util http://www.springframework.org/scheR_361_11845@a/util/spring-util-4.2.xsd http://www.springframework.org/scheR_361_11845@a/mvc http://www.springframework.org/scheR_361_11845@a/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>
2.后台接收的参数在Map<String,String[]>中,如有参数只需在mapper类中方法上只能写Map<String,String[]>形参得到,请求参数!
3.限制大(不太灵活方便),也许有隐藏的bug,仅作为练习.
以上是大佬教程为你收集整理的My jsonp 整合 spring (不太灵活,限制大)全部内容,希望文章能够帮你解决My jsonp 整合 spring (不太灵活,限制大)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。