1. 基础知识
基础知识可以从以下链接地址查阅。
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 mvc中RequestContextHolder的数据将由org.springframework.web.servlet.DispatcherServlet拼装,在没有使用标准spring mvc时需要-->
<!-- org.springframework.web.filter.RequestContextFilter与org.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
相关推荐
AOP拦截器示例,告诉我们初学者理解aop,通过一个银行转帐说明问题。
SpringBoot中自定义拦截器示例代码SpringBoot中自定义拦截器示例代码
拦截器记录日志
通过Mybatis拦截器自动定位慢SQL并记录日志
Mybatis拦截器记录数据更新历史记录到MongoDB的源码,另外需要配置拦截器到mybatis配置文件中。
1.拦截器的使用示例 2.基于反射自己写的拦截器 3.在mvc框架配置即可使用
OkHttp的响应记录拦截器。 记录每个OkHttp调用的url链接与url编码的网络响应信息。
JavaEE 拦截器简单示例源代码
SpringBoot-自定义注解AOP实现及拦截器示例代码
aop+swagger,拦截记录日志功能,博客会有详细描述
spring aop 拦截系统操作日志和异常日志,存储到数据库中的全过程
struts2拦截器可以控制action的执行顺序,这个小示例直接在Tomcat下配置完就可以发布
包含表结构(mysql表创建语句),字段说明与使用;拦截器代码(可根据业务进行修改);
重要-更新通知ResponseEcho(OkLog的服务器端组件)可用,适用于OkHttp的OkLog网络日志记录拦截器。 记录每个OkHttp呼叫的URL链接,其中包含编码的网络呼叫数据。 动机调试Android响应应该更容易。 即使启用了...
NULL 博文链接:https://13lhqiang.iteye.com/blog/865800
自己学习Struts2的拦截器时,从网上找的一个例子,跟着做的源代码,导入到MyEclipse里可以直接使用,注册的小例子,有需要的朋友可以下载参考一下!压缩包里有我从网上跟着做教程的页面。
本篇文章主要介绍了使用Spring MVC拦截器实现日志记录的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
理解拦截器 1.1. 什么是拦截器: 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。 在Webwork的中文文档的解释...
采用ssh框架对权限管理和日志处理的拦截器写法