+ -
当前位置:首页 → 问答吧 → 为什么用hashmap作为返回值,接收到的一直为空?

为什么用hashmap作为返回值,接收到的一直为空?

时间:2011-09-08

来源:互联网

写了一个方法,传入一个String值,查询数据库,返回两个值,写入hashmap,到这里值都是正常的,一return这个hashmap的时候,接收到的总是空,求解

方法代码如下:
public HashMap<String,String> getlasttime(String userid) {
HashMap<String,String> map=new HashMap<String,String>();
String lstime =null;
String sysdate=null;
try {
lstime = this.getSqlMapClientTemplate().queryForObject("demo-module-Msg.getlasttime",userid).toString();
sysdate = this.getSqlMapClientTemplate().queryForObject("demo-module-Msg.getSysDate").toString();
} catch (Exception e) {
e.printStackTrace();
}

map.put("lstime", lstime);
map.put("sysdate", sysdate);
System.out.println(map.get("lstime"));//到这里打印都可以显示值
return map;
}

action代码如下:
System.out.println("last:"+client.getlasttime("393"));很简单,就这么一句,求解

作者: wjzlhm0214   发布时间: 2011-09-08

action代码没有,没法判断你的问题,getlasttime(String userid)这个应该没有问题.............

作者: wula0010   发布时间: 2011-09-08

getlasttime(String userid)这个方法是Dao的方法吗,没有任何问题,返回也没问题。肯定是你的client为null了,所以没有打印出值

作者: yangting_lisa   发布时间: 2011-09-08

肯定是你的client为null了,所以没有打印出值

作者: huangchenliang   发布时间: 2011-09-08


LZ接受到的结果为空是指null,还是hashMap.size==0???

若结果为null:正如楼上所说client为null;

若查出结果集为0那LZ看看数据库中是否存在这条记录;

作者: lvhongfen   发布时间: 2011-09-08

参数没有传递!

作者: yanjinye   发布时间: 2011-09-08

呃。。我的错,代码没贴全,项目前提:struts+spring+ibatis+cxf
cxf-client端调用服务器端方法代码:
  ClassPathXmlApplicationContext context 
= new ClassPathXmlApplicationContext(new String[] 
{"com/demo/cxf/applicationContext-cxf.xml"});

 
CXFService client=(CXFService) context.getBean("client");
System.out.println("last"+client.getlasttime("393"));
  return "success";
现在打印返回的hashmap,显示last{}

作者: wjzlhm0214   发布时间: 2011-09-08

大家有看到最上面方法中有一个sysout,打印的是有值的,可是一旦return map,然后其中的值就没了,嗯,可以这么形容。。。

作者: wjzlhm0214   发布时间: 2011-09-08

对了。。。问题重点,我的返回值是Hashmap格式的,发现个问题,如果用hashmap传参或作为返回值都会为空,为什么?

作者: wjzlhm0214   发布时间: 2011-09-08

你的意思是执行的时候,服务器端可以看到打印的结果,client端显示空?

作者: yueluo5520   发布时间: 2011-09-08

可以这么了解。。。就是在renturn这个map前,我做了一个打印,输出是正常的,有值,但是到client端那边接收到的就是空的了

作者: wjzlhm0214   发布时间: 2011-09-08