Spring mvc aop to get request and response

@Aspect

public class LoggingAspect {


     

    @Before ( "execution(* xxxxxxxxxxx(..))")

    public void logBefore(JoinPoint joinPoint) {

    / /此处可以通过以下方法得到REQUEST,另外也可以通过参数方法得

        HttpServletRequest request = ((ServletRequestAttributes ) RequestContextHolder.getRequestAttributes()).getRequest();

 

        System.out .println("******" +request.getParameter("username" ));

        System.out .println("******" );

        System.out .println("******" );

        System.out .println("******" );

        System.out .println("logBefore() is running!" );

        System.out .println("hijacked : " + joinPoint.getSignature().getName());

        System.out .println("******" );


        //Currently, I just use this method to get response, another way is to use filter to set request and response, then use them in AOP method


        HttpServletResponse response = (HttpServletResponse) joinPoint.getArgs()[0];

         

         


    }

  


}




 在web.xml中配置一个监听

<listener>

        <listener-class>

          org.springframework.web.context.request.RequestContextListener

        </listener-class>

 </listener>

         

你可能感兴趣的:(spring mvc)