2

我正在尝试将我的 HashMap 值存储在会话中,然后我正在使用 JSP 检索它,但我收到一条错误消息

HTTP 状态 500 - org.apache.struts2.dispatcher.SessionMap 无法转换为 java.util.HashMap

知道为什么我会收到这个错误吗?

package com.action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.BreakIterator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.action.GetCon;




public class JanuaryAction implements SessionAware{
private String name;
Map abc = new HashMap();


public Map getAbc() {
return abc;
}

public void setAbc(Map abc) {
this.abc = abc;
}


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Override
public void setSession(Map abc) {
this.abc = (HashMap) abc;

}
public String execute() throws Exception{

String time=getName();
 /*   System.out.println(time);*/
if(time.equals("January 2013")){

abc.put(time,"'2013-01-01' AND OrderDate <= '2013-01-31'" );
}
else if(time.equals("February 2013")){
abc.put(time,"'2013-02-01' AND OrderDate <='2013-02-31'" );
}
else if(time.equals("March 2013")){
abc.put(time,"'2013-03-01' AND OrderDate <='2013-03-31'" );
}
else if(time.equals("April 2013")){
abc.put(time,"'2013-04-01' AND OrderDate <='2013-04-31'" );
}
else if(time.equals("May 2013")){
abc.put(time,"'2013-05-01' AND OrderDate <='2013-05-31'" );
}
else if(time.equals("June 2013")){
abc.put(time,"'2013-06-01' AND OrderDate <='2013-06-05'" );
}
String newtime=(String)abc.get(time);
   /* System.out.println(newtime);*/

Connection con = GetCon.getCon();
Statement statement = con.createStatement();
ResultSet resultset = statement.executeQuery("SELECT MarketPlace,OrderDate, ROUND(SUM(Total),2),  COUNT(*) , ROUND(ROUND(SUM(Total),2)/ COUNT(*),2) FROM vend_printed WHERE OrderDate >=" +newtime+ " GROUP BY OrderDate,MarketPlace") ;
Object value = abc.remove(time);

while(resultset.next()){

String marketplace = resultset.getString(1);

String orderdate = resultset.getString(2);

Double datamount = resultset.getDouble(3);
Integer count= resultset.getInt(4);

Double result=resultset.getDouble(5);

abc.put(0, marketplace);
abc.put(1, orderdate);
abc.put(2, datamount);
abc.put(3, count);
abc.put(4, result);}

return "success";
}}

在我的 JSP 中,我想一一显示循环值。所以我现在正在使用此代码,但它没有显示任何内容。

<s:property value="#session.0" /><br><br><br>
<s:property value="#session.1" /><br><br><br>
<s:property value="#session.2" /><br><br><br>
<s:property value="#session.3" /><br><br><br>
<s:property value="#session.4" /><br><br><br>   
4

3 回答 3

3

问题在这里:

@Override
public void setSession(Map abc) {
  //abc is actually a SessionMap, which is not a HashMap !
  this.abc = (HashMap) abc;
}

因为你认为那abc是一个HashMap,这是错误的。它实际上是一个SessionMap(这就是错误消息所说的)。SessionMap不延伸HashMapAbstractMap直接延伸的延伸Object

请参阅:http ://struts.apache.org/release/2.0.x/struts2-core/apidocs/org/apache/struts2/dispatcher/SessionMap.html

基本上,当您implements SessionAware假设Map将是时HashMap,这绝对不能保证,而且确实不是这样。

我建议你替换属性定义:

HashMap abc = new HashMap();

和:

private Map abc; //Don't initialize it as it is done by the setter.
于 2013-06-21T15:35:59.680 回答
1

问题出在这里:

@Override
public void setSession(Map abc) {
    this.abc = (HashMap) abc;
}

您正在转换 aMap这是HashMap一个具体实现的接口。这种转换是不安全的,因为您不确定它abc是哪种类型。它可以是SessionMap, TreeMap,LinkedHashMap等中的任何一个。

由于setSession已定义为采用 a Map,因此您应该使用它Map或创建一个新的。

public class JanuaryAction implements SessionAware{
    private String name;
    Map abc = new HashMap(); // Use the interface Map instead of a concrete implementation

    @Override
    public void setSession(Map abc) {
        // this.abc = abc; // you can do this
        this.abc = new HashMap(abc); // prefer to do this
    }
}
于 2013-06-21T15:42:26.927 回答
1

这是一个SessionAware实现,使用它

private Map<String, Object> session;

@Override
public void setSession(Map<String, Object> session) {
 this.session = session;
}

execute创建带有值的新集合并放入会话映射中。您的代码存在问题,您正在重用abc最初用于构建查询的相同变量。您应该创建另一个并使用List代替Map来保持要在 JSP 中迭代和显示的项目的顺序。最好创建和使用您应该从中填充的类ResultSet并将其填充到列表中。

public class Market {
  private String marketplace;  
  private String orderdate;  
  private Double datamount;
  private Integer count;  
  private Double result;

  public String getMarketplace() {
    return marketplace;
  }

  public void setMarketplace(String marketplace) {
    this.marketplace = marketplace;
  }

  public String getOrderdate() {
    return orderdate;
  }

  public void setOrderdate(String orderdate) {
    this.orderdate = orderdate;
  }

  public Double getDatamount() {
    return datamount;
  }

  public void setDatamount(Double datamount) {
    this.datamount = datamount;
  }

  public Integer getCount() {
    return count;
  }

  public void setCount(Integer count) {
    this.count = count;
  }

  public Double getResult() {
    return result;
  }

  public void setResult(Double result) {
    this.result = result;
  }

  public Market(String marketplace, String orderdate, Double datamount, Integer count, Double result) {
    this.marketplace = marketplace;
    this.orderdate = orderdate;
    this.datamount = datamount;
    this.count = count;
    this.result = result;
  }

}   

创建属性来保存结果

@Element(value = Market.class)
private List<Market> markets = new ArrayList<>();

public List<Market> getMarkets() {
  return markets;
}

public void setMarkets(List<Market> markets) {
  this.markets = markets;
}

要填充您应该使用此块的列表,我不知道您如何使用连接,所以我没有关闭它。

Connection con = GetCon.getCon();
Statement statement = con.createStatement();
try {
  ResultSet resultset = statement.executeQuery("SELECT MarketPlace,OrderDate, ROUND(SUM(Total),2),  COUNT(*) , ROUND(ROUND(SUM(Total),2)/ COUNT(*),2) FROM vend_printed WHERE OrderDate >=" +newtime+ " GROUP BY OrderDate,MarketPlace") ;
  while(resultset.next()){
  markets.add(new Market(
    resultset.getString(1),
    resultset.getString(2),
    resultset.getDouble(3),
    resultset.getInt(4),
    resultset.getDouble(5)
  ));
  }
} finally {
  try {
    statement.close();
  } catch (SQLException e) {}
}

}

现在将结果保留在会话中

session.put("markets", markets);

在 JSP 中使用iterator标签,您不需要迭代会话对象,只需从操作中获取它。

<s:iterator value="markets">
  Marketplace: <s:property value="marketplace" /><br> 
  Orderdate: <s:property value="orderdate" /><br>  
  Datamount: <s:property value="datamount" /><br>
  Count: <s:property value="count" /><br>  
  Result: <s:property value="result" /><br>
  <hr>
</s:iterator>
于 2013-06-21T15:48:53.000 回答