`

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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics