我正在尝试将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>
答案 0 :(得分: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
不会延伸HashMap
,会延伸AbstractMap
直接延伸Object
。
基本上,当您implements SessionAware
时,您认为Map
将是HashMap
,这绝对不能保证,事实并非如此。
我建议你替换属性定义:
HashMap abc = new HashMap();
使用:
private Map abc; //Don't initialize it as it is done by the setter.
答案 1 :(得分:1)
问题在于:
@Override
public void setSession(Map abc) {
this.abc = (HashMap) abc;
}
您正在投射Map
,这是HashMap
的一个接口,这是一个具体的实现。此演员表不安全,因为您不确定abc
是哪种类型。它可以是SessionMap
,TreeMap
,LinkedHashMap
等中的任何一种。
由于setSession
已被定义为Map
,因此您应该使用Map
或创建一个新的。{/ p>
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
}
}
答案 2 :(得分:1)
这是SessionAware
实现,使用它
private Map<String, Object> session;
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
<{1>}在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>