使用struts2获取session出现异常

ctionContext ctx = ActionContext.getContext();

HttpSession session = (HttpSession) ctx.getSession();   

session.setAttribute("buyer", buyerManager.getBuyer(username, password));

=================================================================

报错java.lang.ClassCastException: org.apache.struts2.dispatcher.SessionMap cannot be cast to javax.servlet.http.HttpSession

解决方法:

ActionContext.getContext().getSession();

返回值是map类型,Map<String,Object>

取session的真实类型可以:

HttpServletRequest request = ServletActionContext.getRequest();

HttpSession session = request.getSession();

如果要取map类型(手动初始化):

Map session = ActionContext.getContext().getSession();

session.put("sessionName","sessionValue");

猜你喜欢

转载自blog.csdn.net/thomassamul/article/details/81153000
今日推荐