JFinal interceptor - Deal with session attributes

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;

public class SessionInterceptor implements Interceptor
{
   private static final String SERVER_ADDR = "serverAddr";

   @Override
   public void intercept(ActionInvocation ai)
   {
      ai.invoke();

      HttpSession session = ai.getController().getRequest().getSession();
      HttpServletRequest request = ai.getController().getRequest();
      request.setAttribute(SERVER_ADDR, request.getServerName());

      if (session != null)
      {
         Enumeration< ? > e = session.getAttributeNames();
         if (e != null)
         {
            String key = null;
            while (e.hasMoreElements())
            {
               key = e.nextElement().toString();
               request.setAttribute(key, session.getAttribute(key));
            }
         }
      }
   }
}


你可能感兴趣的:(session,Interceptor,jFinal)