struts2 全局拦截器,显示请求方法和参数

后台系统中应该需要一个功能那就是将每个请求的url地址和请求的参数log出来,方便系统调试和bug追踪,使用struts2时可以使用struts2的全局拦截器实现此功能:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
 
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 
/**
  * 全局方法拦截器,用于log方法调用以及参数信息
  */
public class GloableLogInterceptor extends AbstractInterceptor {
 
     private static final long serialVersionUID = 1L;
 
     private Logger log = Logger.getLogger( this .getClass());
 
     @Override
     public String intercept(ActionInvocation invocation) throws Exception {
 
         if (log.isDebugEnabled()) {
             HttpServletRequest request = (HttpServletRequest) invocation
                     .getInvocationContext().get(
                             ServletActionContext.HTTP_REQUEST);
             this .logParameters(request);
         }
 
         return invocation.invoke();
     }
 
     /**
      *
      * @param request
      */
     private void logParameters(HttpServletRequest request) {
         
         Map<string, string[]= "" > params = request.getParameterMap();
 
         if ( null == params || params.size() == 0 ) {
             return ;
         }
 
         Set<string> keys = params.keySet();
         Iterator<string> keysIt = keys.iterator();
 
         StringBuffer container = new StringBuffer();
         container.append( "requestUrl[url:" )
                 .append(request.getRequestURL().toString()).append( "]" )
                 .append( ",paremeters:[" );
 
         while (keysIt.hasNext()) {
 
             String key = keysIt.next();
             String[] values = params.get(key);
 
             StringBuffer str = new StringBuffer();
             str.append(key).append( "=" );
 
             if (values.length > 1 ) {
                 str.append( "{" );
 
                 for (String value : values) {
                     str.append(value).append( "," );
                 }
 
                 this .removeLastCharacter(str);
                 str.append( "}" );
             } else
                 str.append(values[ 0 ]);
 
             str.append( "," );
             container.append(str.toString());
         }
 
         this .removeLastCharacter(container);
         container.append( "]" );
         log.debug(container.toString());
     }
 
     private void removeLastCharacter(StringBuffer buff) {
         int len = buff.length();
         buff.replace(len - 1 , len, StringUtils.EMPTY);
     }
}
</string></string></string,>

这时我们还需要自定义拦截器链,这样就不需要在每个package中声明使用此拦截器,只要将需要的package继承我们带有日志拦截功能的拦截器即可!

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<interceptors>
     <!--全局日志拦截器-->
     <interceptor name= "gloableLogInterceptor" class = "gloableLogInterceptor" ></interceptor>
 
     <interceptor-stack name= "defaultStack" >
         <interceptor-ref name= "exception" >
         <interceptor-ref name= "alias" >
         <interceptor-ref name= "servletConfig" >
         <interceptor-ref name= "i18n" >
         <interceptor-ref name= "prepare" >
         <interceptor-ref name= "chain" >
         <interceptor-ref name= "scopedModelDriven" >
         <interceptor-ref name= "modelDriven" >
         <interceptor-ref name= "fileUpload" >
         <interceptor-ref name= "checkbox" >
         <interceptor-ref name= "multiselect" >
         <interceptor-ref name= "staticParams" >
         <interceptor-ref name= "actionMappingParams" >
         <interceptor-ref name= "params" >
             <param name= "excludeParams" >^action:.*,^method:.*
         </interceptor-ref>
         <!-- 在注入参数后进行拦截 -->
         <interceptor-ref name= "gloableLogInterceptor" ></interceptor-ref>
         <interceptor-ref name= "tokenInterceptor" ></interceptor-ref>
         <interceptor-ref name= "conversionError" >
         <interceptor-ref name= "validation" >
             <param name= "excludeMethods" >input,back,cancel,browse
         </interceptor-ref>
         <interceptor-ref name= "workflow" >
             <param name= "excludeMethods" >input,back,cancel,browse
         </interceptor-ref>
         <interceptor-ref name= "debugging" >
         <interceptor-ref name= "deprecation" >
     </interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-ref></interceptor-stack>
</interceptors>
 
< default -interceptor-ref name= "defaultStack" ></ default -interceptor-ref>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值