0

我想为HashMap从另一个类传递的每个“服务”参数存储查询结果,然后将它们存储到应用程序范围application.setAttribute(),如果服务存在于应用程序范围内,我可以从中获取这些值 application.getAttribute()。每个服务要存储 3 列。如果它们存在于应用程序范围内,如何根据服务参数获取这 3 个列值?请建议如何在应用程序范围内存储值(如果我在以下代码中做错了)。

 service=request.getParameter("service");   
 Map<String,String> FreetextMap=new HashMap<String,String>();

if(application.getAttribute(service)==null )
{
query = "SELECT aUnique,aTitle,aBody FROM FreeTexts WHERE service='" +service+ "'     
rs = DBUtils.getRs(con,query);
if (rs.next()) {
clientFreetextMap.put("unique", rs.getString("aUnique"));
clientFreetextMap.put("body",rs.getString("aBody"));
clientFreetextMap.put("txt",rs.getString("aTitle"));
application.setAttribute(service,clientFreetextMap);

 }

以下代码负责从应用程序范围获取值,但它不能,因为我知道我缺少一些东西来声明服务。我不知道在哪里写服务?如何获取有关服务参数的值?

    else 
     {
   txt=(String) application.getAttribute(clientFreetextMap.get("txt"));
   uniqueid=(String) application.getAttribute(clientFreetextMap.get("unique"));
   adsbody=(String) application.getAttribute(clientFreetextMap.get("body"));
     }
4

1 回答 1

1

您将clientFreetextMap自身存储在if块中。您应该Mapelse块中得到相同的结果,然后从中获取所需的数据。尝试这样的事情:

clientFreetextMap = (Map)application.getAttribute(service);
txt = clientFreetextMap.get("txt");
uniqueid = clientFreetextMap.get("unique");
adsbody = clientFreetextMap.get("body");
于 2013-05-19T11:16:59.460 回答