我正在尝试将一些旧代码从WebSphere迁移到Tomcat。旧代码使用Spring 3.2,现在我将JAR升级到5.2.2。但是以某种方式对象值不会持久。
我的控制器类是:
@Controller
@Scope("session")
public class OperationController {
private GUIDataObject guiDO = null;
/**
* Constructor
*/
public OperationController() {
}
@RequestMapping(value="/readDataSource")
@ResponseBody
public String readDataSource() {
try {
String[] sources = guiDO.getDataSources();
.
.
.
Code to work on Array sources
.
.
.
return "ok";
} catch (Exception e) {
return "Error: " + e.getMessage();
}
}
/**
* Set the data sources in the Data Storage Area - these are passed as a "parameter" map
* in the request.
* @param webRequest : WebRequest which parameter map can be pulled from
* @return "ok"
*/
@RequestMapping(value="/setDataSources")
@ResponseBody
public String setDataSources(WebRequest webRequest) {
guiDO.setDatasources(webRequest.getParameterMap());
return "ok";
}
.
.
.
Lots of other code.
.
.
.
}
值存储在对象中
public class GUIDataObject {
private String service;
private String uniqueProcessId;
private String userId;
private String vendor;
// record the data sources to read from
private Map<String, String[]> dataSources = null;
public GUIDataObject(String service, String uniqueProcessId, String userId, String vendor) {
super();
this.service = service;
this.uniqueProcessId = uniqueProcessId;
this.userId = userId;
this.vendor = vendor;
}
public void setDatasources(Map<String, String[]> datasources) {
this.dataSources = datasources;
}
public String[] getDataSources() throws Exception {
if (this.dataSources == null) {
throw new Exception("No datasources have been set from the GUI");
}
if (!this.dataSources.containsKey("source")) {
throw new Exception("No datasources have been set from the GUI");
}
return this.dataSources.get("source");
}
.
.
.
Lots of methods.
.
.
.
}
现在,我的问题是dataSources
地图的设置越来越好。但是,当获取值时,它们将返回空。它在第二个if块中出错,因此我至少可以说它不是null。对象中还有其他Maps / Strings,但是我无法真正确定它们是否已正确设置,因为这是第一个被命中的方法,此后它会出错。我可以看到在构造函数中初始化的值被保留得很好。所以真的不能出错了。
相同的代码在WebSphere和Spring 3.2上运行良好。现在,我不确定是否需要进行任何新配置才能使其正常工作。由于3.2很老。任何帮助,将不胜感激。
答案 0 :(得分:1)
问题是webRequest.getParameterMap()
在WebSphere和Tomcat中的工作方式。在WebSphere中,它返回一个具体的HashTable
。但是在Tomcat中,它返回org.apache.catalina.util.ParameterMap
的子类HashMap
。而且他们只是不混在一起。甚至投射也会抛出ClassCastException
。
通过将dataSources
更改为HashMap
使它起作用。
private HashMap<String, String[]> dataSources = null;
以及设置方法为:
public void setDatasources(Map<String, String[]> datasources) {
if (this.dataSources == null) {
this.dataSources = new HashMap<String, String[]>();
this.dataSources.putAll(datasources);
} else {
this.dataSources.putAll(datasources);
}
可能我可以将dataSources
保留为Map
,但它仍然可以正常工作。但是我没有尝试。