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. 在线考试系统(Online Exam System)--ASP.NET

    用户设计 -|学生 -|老师 -|管理员 学生结构设计 -|个人信息管理 -|修改个人信息 -|修改登录密码 -|选课中心 -|显示所有老师所开课的信息可进行选课 -|显示自己已选课程 -|在线考试 ...

  2. h5移动端滑动的细节

    1.获取手指滑动的长度: var hasTouch = 'ontouchstart' in window && !isTouchPad, _start:function(e){ var ...

  3. Hermite Curve

    http://paulbourke.net/miscellaneous/interpolation/ http://fivedots.coe.psu.ac.th/Software.coe/Java%2 ...

  4. top命令详解(转,详细)

    来源:脚本之家(http://www.jb51.net/article/40807.htm) 本文通过一个运行中的WEB服务器的top监控截图,讲述top视图中的各种数据的含义,还包括视图中各进程(任 ...

  5. ElasticSearch之一——索引

    ElasticSearch索引 ElasticSearch 是一个分布式可扩展的实时搜索引擎,它建立在开源搜索引擎框架Apache Lucene基础上.ElasticSearch 不但包括了全文搜索功 ...

  6. PHP与Javascript的混合测试

    js调用php <?php $num=88; ?> <script> var a = <?php echo $num;?>; alert(a); </scri ...

  7. javascript 之Object内置对象

    Object.defineProperty(obj, prop, descriptor)

  8. Java中native关键字

    Java中native关键字 标签: Java 2016-08-17 11:44 54551人阅读 评论(0) 顶(23453) 收藏(33546)   今日在hibernate源代码中遇到了nati ...

  9. abrtd是什么进程

    abrtd 是一个守护进程监控的应用程序崩溃.当发生崩溃时,它将收集的崩溃(核心文件的命令行, etc .)application ,并采取措施根据类型崩溃并根据 abrt.conf config 文 ...

  10. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...