java mvc 获取session_在spring mvc中获取session值问题

验证用户登录代码如下:

@Controller

@RequestMapping("/userInfoController")

@SessionAttributes({"userInfo"})

public class userInfoController {

@Autowired(required=true)

private UserInfoApp userInfoApp;

@RequestMapping(value="/login" ,method = RequestMethod.POST)

public String login(TUserInfo userInfo,HttpServletRequest request,ModelMap map){

TUserInfo result = userInfoApp.login(userInfo);

if(null!=result){

int role=result.getRole();

if(1==role){

}else if(2==role){

}

map.addAttribute("userInfo", result);

return "index";

}else{

request.setAttribute("msg", "用户名或密码错误");

return "login/login";

}

}

}

自己写的过滤器代码如下:

@SessionAttributes({"userInfo"})

public class SessionFilter implements Filter{

private FilterConfig filterConfig;

private String excludePath;

private String redirectUrl;

private String loginUrl;

public void destroy() {

this.filterConfig = null;

}

public void doFilter(ServletRequest req, ServletResponse res,

FilterChain chain) throws IOException, ServletException {

ModelMap map;

HttpServletRequest request = (HttpServletRequest)req;

HttpServletResponse response = (HttpServletResponse) res;

String url = request.getRequestURL().toString();

String controller = url.substring(url.lastIndexOf("/")+1);

String[] exclude = excludePath.split(",");

boolean flag = false;

for(String str:exclude){

if(url.indexOf(str)!=-1){

flag = true;

break;

}

}

if(flag){

chain.doFilter(request, response);

}else{

String tag = (String)SessionTimeOutListener.threadLocal.get();

String userInfo =(String) request.getSession().getAttribute("userInfo");

if(null!=tag && tag.equals("timeOut")){

response.sendRedirect(request.getContextPath()+redirectUrl);

return ;

}else if(null==tag && null==userInfo){

response.sendRedirect(request.getContextPath()+loginUrl);

return ;

}else if(null!=userInfo){

chain.doFilter(request, response);

}

}

chain.doFilter(request, response);

}

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

this.excludePath = this.filterConfig.getInitParameter("excludePath");

this.redirectUrl = this.filterConfig.getInitParameter("redirectUrl");

this.loginUrl =this.filterConfig.getInitParameter("loginUrl");

}

}

为什么在过滤器中 String userInfo =(String) request.getSession().getAttribute("userInfo");

这个userInfo总取不到值,获得的一直是null,请高手指点,在线等!谢谢了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值