struts2 拦截器Class

 

个人记录

1 拦截器类继承MethodFilterInterceptor

 

 

 

 
   @Override
   protected String doIntercept(ActionInvocation invocation) throws Exception {
		ActionContext ac = invocation.getInvocationContext();
		Map<String,Object> session = ac.getSession();
		Integer userId = (Integer)session.get("userId");
		HttpServletRequest request = (HttpServletRequest)ac.get(StrutsStatics.HTTP_REQUEST);
		String uri = request.getServletPath();
		int decimalIndex = uri.lastIndexOf(".");
		uri = uri.substring(0, decimalIndex); 
		System.out.println(uri);
		if("/login".equals(uri) || "/logout".equals(uri) ){
			return invocation.invoke();
		}else if(userId == null ){
			return "loginPage";
		}else{
			List<String> priList = (List<String>)session.get("userPrivilege");
			if(priList.contains(uri)){
				return invocation.invoke();
			}else{
				if(uri.endsWith("ajax")){
					HttpServletResponse response = (HttpServletResponse)ac.get(StrutsStatics.HTTP_RESPONSE);
					response.setContentType("text/html;charset=UTF-8");
					PrintWriter out = response.getWriter();
					String message = "没有权限";
					out.println(message);
					out.flush();
					out.close();
					return null;
				}else{
					return "authFail";
				}
			}
		}
	}




 

 

 

 

 

 

 

 

你可能感兴趣的:(java,struts)