-
后台系统中应该需要一个功能那就是将每个请求的url地址和请求的参数log出来,方便系统调试和bug追踪,使用struts2时可以使用struts2的全局拦截器实现此功能:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990import
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继承我们带有日志拦截功能的拦截器即可!1234567891011121314151617181920212223242526272829303132333435363738<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>
struts2 全局拦截器,显示请求方法和参数
最新推荐文章于 2021-09-29 23:06:34 发布