read方法中调用的response对象是父类BaseController的一个成员变量.

spring默认bean的生命周期Score为singleton单例模式.

当多线程并发使用同一bean, 并使用bean中的同一成员变量时会有问题. 比如下例中的成员变量response

以下是服务端跨域代码.

@Controller
@Scope("request")
@RequestMapping("/CrossDomain")
public class CrossDomainController extends BaseController { @RequestMapping("/read")
@ControllerLog(description = "跨域请求")
public void read(@RequestParam Map<String, Object> params) throws IOException {
String url = (String) params.get("url");
if(BlankUtil.isNotEmpty(url)) {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
// 设置请求的header
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (String key : params.keySet()) {
nvps.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse httpResponse = httpClient.execute(httpPost);
String result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
if("xml".equalsIgnoreCase(String.valueOf(params.get("type")))) {
response.setContentType("text/xml; charset=UTF-8");
}
try {
System.err.println("---------->" + String.valueOf(params.get("type")) + result.length());
PrintWriter out = response.getWriter();
out.write(result);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} }

修改Bean的生命周期scope配置为request或prototype即可.

JS代码(请求Java后台得到远程服务器xml文本内容,  然后在js端转换成xmldoc对象)

function crossDomainloadXmlFile(config) {
var doc = null;
Ext.Ajax.request({
url : config.url, // 请求地址
params : config.params, // 请求参数
timeout: config.timeout || 30000,
async : false,//同步请求
method : 'post', // 方法
callback : function(options, success, response) {
if(!success) {
if(top.checkTimeoutAndRelogin(response.status)) {
top.Ext.MessageBox.alert('错误提示', response.message);
}
return;
}
console.log('crossDomainloadXmlFile:' + response.responseText)
doc = parseXmlString(response.responseText);
}
});
return doc;
} function parseXmlString(xmlString) {
var doc;
if (window.ActiveXObject) {
doc = new ActiveXObject("Microsoft.XMLDOM");
doc.setProperty("SelectionLanguage", "XPath");
doc.async = false;
doc.loadXML(xmlString);
}
// load XML string code for Mozilla, Firefox, Opera, etc.
else {
var parser = new DOMParser();
doc = parser.parseFromString(xmlString, "text/xml");
} return doc.documentElement;
}

EXT组件:

/**
* Created by huangbaidong on 2016/6/30.
* 简单数据字典通用Store,
* 外部调用传入参数dictionaryId作为过滤条件.
* 使用案例:
store: Ext.create('app.component.sys.dictionary.SimpleDictionaryXmlStore', {
dictionaryId : 1
})
*/
Ext.define('app.component.sys.dictionary.SimpleDictionaryXmlStore', {
extend: 'Ext.data.XmlStore',
storeId :'simpleDictionaryStore',
dictionaryId : 0,
constructor : function(config) {
var me = this;
Ext.apply(config, {
fields: [{
name: 'id',
mapping: '@id'
}, {
name: 'name',
mapping: '@name'
}, {
name: 'dictionaryId',
mapping: '@dictionaryId'
}],
data:(function(){
var array = [];
Ext.each(top.simpleDictionaryStore.getData().items, function(item) {
if(item.data.dictionaryId == config.dictionaryId) {
array.push(item);
}
})
return array;
})()
});
app.component.sys.dictionary.SimpleDictionaryXmlStore.superclass.constructor.call(this, config);
}
});
/**
* 跨域获取数据字典, 并缓存为全局单例Store
* @type {Ext.data.Store}
*/
top.simpleDictionaryStore = Ext.create('Ext.data.Store', {
autoLoad : true,
fields: [{
name: 'id',
mapping: '@id'
}, {
name: 'name',
mapping: '@name'
}, {
name: 'dictionaryId',
mapping: '@dictionaryId'
}],
proxy: {
type: 'ajax',
url: '../CrossDomain/read',
reader: {
type: 'xml',
record: 'simpleDictionary',
idProperty: '@id',
totalRecords: '@total'
},
extraParams:{//服务端跨域处理
url: DICTIONARY_URL,
type : 'xml'
}
}
});

spring ext 跨域的更多相关文章

  1. spring boot跨域请求访问配置以及spring security中配置失效的原理解析

    一.同源策略 同源策略[same origin policy]是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源. 同源策略是浏览器安全的基石. 什么是源 源[orig ...

  2. SpringBoot 优雅配置跨域多种方式及Spring Security跨域访问配置的坑

    前言 最近在做项目的时候,基于前后端分离的权限管理系统,后台使用 Spring Security 作为权限控制管理, 然后在前端接口访问时候涉及到跨域,但我怎么配置跨域也没有生效,这里有一个坑,在使用 ...

  3. spring boot 跨域访问处理

    问题场景:由于项目中使用到跨域访问,今天也得到高人指点,所以写出来分享给大家.可能是考虑到前后端分离,前端后端服务器不在一台机器上,出现这种跨域访问的情况.正常情况下本地访问是没有问题,但是遇到这种非 ...

  4. spring boot跨域设置

    定义 跨域是指从一个域名的网页去请求另一个域名的资源 跨域背景 限制原因 如果一个网页可以随意地访问另外一个网站的资源,那么就有可能在客户完全不知情的情况下出现安全问题 为什么要跨域 公司内部有多个不 ...

  5. SPRING BOOT跨域访问处理

    尊重原创:http://blog.csdn.net/ruiguang21/article/details/77878933 问题场景:由于项目中使用到跨域访问,今天也得到高人指点,所以写出来分享给大家 ...

  6. spring boot跨域问题

    跨域是指不同域名之间相互访问.跨域,指的是浏览器不能执行其他网站的脚本.它是浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制.也就是如果在A网站中,我们希望使用Ajax来获得B网站 ...

  7. spring boot 跨域请求

    场景 网站localhost:56338要访问网站localhost:3001的服务 在网站localhost:3001中增加CORS相关Java Config @Configuration @Ord ...

  8. 关于Spring MVC跨域

    1.Sping MVC 3.X跨域 关于跨域问题,主要用的比较多的是cros跨域. 详细介绍请看https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Ac ...

  9. spring mvc 跨域问题。。。解决

    官方推荐方式: http://spring.io/blog/2015/06/08/cors-support-in-spring-framework 方式1: $.ajax({ //前台:常规写法.注意 ...

随机推荐

  1. Spot光照资料

    http://forums.autodesk.com/t5/FBX-SDK/EmissiveFactor-AmbientFactor-DiffuseFactor/td-p/4230572http:// ...

  2. ecshop if标签,超过N条,就输出记录 elseif、库存显示方式

    <!--商品详情右侧 相关商品推荐--> <!-- {if $related_goods} --> <!--{foreach from=$related_goods it ...

  3. linux win 通用的获取Mac的方法

    经测试下面方法获取Mac跨平台 protected override void OnLoad(EventArgs e) { Response.Write(string.Join("<b ...

  4. VS插件开发,启用实验室环境

    启用外部程序: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe 命令行参数 /rootsuffix ...

  5. SVN Tree Conflict 的分析

    所谓Tree Confict,就是至少有一个人修改了目录结构,包括文件或者文件所在目录的改名.删除.移动.然后Update或Merge的时候就报了Tree Conflict. 介绍一下概念Delete ...

  6. css3实现渐变的iPhone滑动解锁效果

    先贴代码 <!DOCTYPE html> <html> <head> <style> p{ width:50%; margin:0 auto; line ...

  7. php开发工具之火狐浏览器插件

    相信做开发的都有一种火狐情怀吧!  下面来介绍下一些自己在php开发工程中用到几个火狐浏览器插件. 1.[firebug]: 这个插件可以说是一个神奇,功能不用过对介绍. 2.[hostAdmin]: ...

  8. HDOJ 4739 Zhuge Liang&#39;s Mines

    Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. QT共享库的创建与调用(初级)(附:UI界面不能被改变的其中一个原因)

    背景: 最近在做的一个项目其中一部分既是实现PC与下位机的USB通信.windows平台下已经完成,现需移植到linux平台下. 在linux系统中,通过一段时间的工作,设备已被配置成hid类(后续再 ...

  10. Ubuntu 12 升级 SVN 1.6 到 1.8 版本

    在 Ubuntu 12 中使用 PhpStorm 10.x,CheckOut项目后,Event Log 提示: Subversion command line client version is to ...