以下是我的 控制器 代码:
我已经在下面的 "doLogin" 方法中将我的 对象存储在一个 map 中,我正在尝试在我的 "logout" 函数中访问它,但是当我使用 "map.get(key)" 获取我的会话属性值时,我得到了 null 值。
@Controller
@SessionAttributes(value={"session1"})
public class CredentialsController {
@Autowired
private Authentication authenticationDao;
@Autowired
private User userDao;
@RequestMapping(value="/start",method=RequestMethod.GET) //Default Method
public String doStart(@ModelAttribute CredentialsBean credentialsBean)
{
return "login";
}
@RequestMapping(value="/login",method=RequestMethod.GET) //Default Method
public String doLogin(@ModelAttribute CredentialsBean credentialsBean,Map<String,Object> map)
{
String result="";
if(credentialsBean!=null){
if(authenticationDao.authenticate(credentialsBean)){
String userType=authenticationDao.authorize(credentialsBean.getUserID());
if(userType.equalsIgnoreCase("A")){
CredentialsBean cBean= authenticationDao.changeLoginStatus(credentialsBean, 1);
map.put("session1",cBean); ----->Here I am putting the object inside a map .
result= "admin";
//map.put("username",credentialsBean.getProfileBean().getFirstName());
}
else{
CredentialsBean cBean=authenticationDao.changeLoginStatus(credentialsBean, 1);
map.put("session1",cBean.getUserID());
//System.out.println(cBean.getUserID());
result= "customer";
//map.put("username",credentialsBean.getProfileBean().getFirstName());
}
}
else{
result="ERROR";
}
}
return result;
}
@RequestMapping(value="/logout",method=RequestMethod.GET) //Default Method
public String doLogout(Map<String,Object > map)
{
CredentialsBean credentialsBean=(CredentialsBean)map.get("session1");
//System.out.println(userID);
System.out.println(credentialsBean.getUserID());
if(credentialsBean!=null){
if(userDao.logout(credentialsBean.getUserID())){
return "logout";
}
else{
return "error1";
}
}
else{
return "error";
}
}
}
HttpSession
并将你的地图保存在那里。然后在注销时从会话中检索它并获取你的值。另外,我建议你看一下 Spring Security 模块。 - Enigo