`
conkeyn
  • 浏览: 1504414 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

动作日志记录拦截器示例

 
阅读更多

动作日志记录拦截器示例

 

1.  基础知识

基础知识可以从以下链接地址查阅。

1、注解(Annotation)基本概念

2、注解(Annotation)自定义注解入门

3、注解(Annotation--注解处理器

2.  项目中的动作日志记录拦截器编码步骤

2.1.  创建Action

示例中使用的spring mvc是框架,与使用struts2框架大同小异。

 

@Controller

@RequestMapping(value="/blog")

publicclass BlogController {

 

   @Resource

   private BlogService blogService;

  

   @RequestMapping(value="/insertblog",method=RequestMethod.GET)

   @Action("插入微博") //定义动作日志

   public ModelAndView insertBlog(HttpServletRequest request,

          HttpServletResponse response, @RequestParam String name){

      Blog blog = new Blog();

      blog.setName(name);

      blogService.insertBlog(blog);

      returnnew ModelAndView("blog/insertblog");

   }

}

2.2.  创建注解类

package org.spring.example.log;

 

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Inherited;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

 

@Target({ElementType.METHOD,ElementType.TYPE})//应用于类型和方法

@Documented//标记是否可以被文档化java doc

@Retention(RetentionPolicy.RUNTIME)//指明保留周期(生命周期)要运行时期。

@Inherited// 要使被注解的父类也可以应用到子类中

public@interfaceAction {

   public String value() default"";//必须指明不为空的默认值,

}

 

2.3.  创建拦截器

 

import java.lang.reflect.Method;

import javax.servlet.http.HttpSession;

import org.aspectj.lang.ProceedingJoinPoint;

import org.spring.example.log.Action;

import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

 

publicclass ActionLogInterceptor {

 

   // 此方法的方法名自定义,参数类型为org.aspectj.lang.ProceedingJoinPoint,返回类型为Object

   public Object doActionLog(ProceedingJoinPoint point) throws Throwable {

      String actionDesc = null;

      String methodName = point.getSignature().getName();// 读取切点的方法名称

      Class clazz = point.getTarget().getClass();

      for (Method method : clazz.getDeclaredMethods()) {

          if (method.getName().equals(methodName)) {

             //判断是有Action注解标识

             if(method.isAnnotationPresent(Action.class)){

                //读取action注解标识的值

                Action action = method.getAnnotation(Action.class);

                actionDesc = action.value();

             }

          }

      }

      // RequestContextHolder类的静态方法取得当前用户

      ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

      HttpSession session = attr.getRequest().getSession();

 

      // 输出当前用户的会话ID

      System.out.println("session id: " + session.getId() + ", actionDesc: "

             + actionDesc);

 

      return point.proceed();

   }

}

 

2.4.  配置web.xml

配置web.xml,主要是为了获取当前是请求,并从当前请求中取得session对象。

 

<filter>

        <!-- 在标准的spring mvcRequestContextHolder的数据将由org.springframework.web.servlet.DispatcherServlet拼装,在没有使用标准spring mvc时需要-->

        <!-- org.springframework.web.filter.RequestContextFilterorg.springframework.web.context.request.RequestContextHolder搭配使用 -->

        <filter-name>RequestContextFilter</filter-name>

        <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>

    </filter>

   

    <filter-mapping>

        <filter-name>RequestContextFilter</filter-name>

        <url-pattern>*</url-pattern>

    </filter-mapping>

2.5.  配置拦截器

<bean id="actionLogInterceptor" class="org.spring.example.interceptor.ActionLogInterceptor" />

   <aop:config proxy-target-class="true">

       <!-- 使用执行表达式定义规则 -->

      <!--aop:pointcut expression="execution(* org.spring.example.web..*(..))" id="actionLogPointCut" /  -->

      <!-- 使用@annotation表达式写义规则 -->

      <aop:pointcut expression="@annotation(org.spring.example.log.Action)" id="actionLogPointCut" />

      <aop:aspect ref="actionLogInterceptor">

          <aop:around method="doActionLog" pointcut-ref="actionLogPointCut" />

      </aop:aspect>

   </aop:config>

 

3.  测试

http://localhost:8080/springmvc/blog/insertblog.do?name=j

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics