当前位置:编程学习 > JAVA >>

关于Map<String,String> session的问题


public class Login extends ActionSupport {
private IUserService userService;
private String userName;
private String password;

/**
 * 如果用户名和密码匹配则登陆成功
 * 并且将当前登录的用户的ID保存在session对象中
 */
public String execute() throws Exception{
//得到当前session
Map<String,String> session = ActionContext.getContext().getSession();

//有用户名得到用户ID
String userId = userService.getUserIdByName(userName);
session.put("userId", userId);

return SUCCESS;
}

Map<String,String> session = ActionContext.getContext().getSession();
这一行的ActionContext.getContext().getSession();有错误,不知道为什么
Struts2封装了Map,map的操作被自动地映射成了对session的操作,为什么报错呢?Struts拦截器还需要在配置吗?我的是这样的
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

在线等哈~ --------------------编程问答-------------------- 错误结果显示
Type mismatch: cannot convert from Map<String,Object> to Map<String,String> --------------------编程问答--------------------  Map<String,Object> session = ActionContext.getContext().getSession(); 改称这样试一试~~ --------------------编程问答--------------------
引用 2 楼 a236036779 的回复:
 Map<String,Object> session = ActionContext.getContext().getSession(); 改称这样试一试~~

可以倒是可以,但是我还是不太明白具体原因哈,
书里例子的代码就是<String,String>为什么到我这里就出问题了 --------------------编程问答--------------------
引用 3 楼 hellobeifeng 的回复:
Quote: 引用 2 楼 a236036779 的回复:

 Map<String,Object> session = ActionContext.getContext().getSession(); 改称这样试一试~~

可以倒是可以,但是我还是不太明白具体原因哈,
书里例子的代码就是<String,String>为什么到我这里就出问题了


那要看看你是怎么封装的代码了~在映射中可能已经指定了Map<String,Object> --------------------编程问答--------------------
引用 4 楼 a236036779 的回复:
Quote: 引用 3 楼 hellobeifeng 的回复:

Quote: 引用 2 楼 a236036779 的回复:

 Map<String,Object> session = ActionContext.getContext().getSession(); 改称这样试一试~~

可以倒是可以,但是我还是不太明白具体原因哈,
书里例子的代码就是<String,String>为什么到我这里就出问题了


那要看看你是怎么封装的代码了~在映射中可能已经指定了Map<String,Object>

说道点子上了,兄弟,那么这个映射在哪里定义的呢? --------------------编程问答-------------------- 我顶!我顶!我顶! --------------------编程问答-------------------- 我再顶!我再顶!我再顶! --------------------编程问答--------------------
引用 6 楼 hellobeifeng 的回复:
我顶!我顶!我顶!


这个你可能要去看源码了~~~因为是在别人封装好的了~在jar里面
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,