2
回答
在spring mvc中获取session值问题
终于搞明白,存储TCO原来是这样算的>>>   

验证用户登录代码如下:

@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,请高手指点,在线等!谢谢了

举报
magelite10
发帖于5年前 2回/9K+阅
顶部