在下面的代码中,学习是一个学习实例,我想将它保存到http会话中
learning = new Learning(learningContext);
HttpSession webSession = request.getSession();
webSession.setAttribute("learning", learning);
Learning learnTest = webSession.getAttribute("learning");当我运行下面的代码时,我得到:
Incompatible types
Learning learnTest = webSession.getAttribute("learning")
required: Learning
found: Object是否可以将非泛型对象保存到会话中?还有其他方法可以做到这一点吗?
发布于 2016-03-04 08:35:49
你需要像这样投射物体-
Learning learnTest = (Learning) webSession.getAttribute("learning")您需要转换为特定类型,因为getAttribute的返回类型是对象。
发布于 2016-03-04 08:39:04
HttpSession.getAttribute的javadoc说:
java.lang.Object getAttribute(java.lang.String名称)返回在此会话中使用指定名称绑定的对象,如果没有在该名称下绑定对象,则返回null。
我的意思是,建筑总是会返回一个简单的物体。正如您所知道的(或希望)您已经在那里放置了一个Learning,您只需执行一个显式的强制转换:
Learning learnTest = (Learning) webSession.getAttribute("learning");https://stackoverflow.com/questions/35791337
复制相似问题