Struts2 global interceptor that displays request methods and parameters
In the background system, a function should be required to log the url address and parameters of each request to facilitate system debugging and bug tracking, when using struts2, you can use the global interceptor of struts2 to implement this function:
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;/*** global method interceptor FOR log method calls and parameter information */public class GloableLogInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 1L; private Logger log = Logger. getLogger (this. getClass (); @ Overridepublic 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
Params = request. getParameterMap (); if (null = params | params. size () = 0) {return;} Set
Keys = params. keySet (); Iterator
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 ("}");} elsestr. 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 );}}
At this time, we also need to customize the interceptor chain, so that we do not need to declare to use this interceptor in each package, as long as we need to inherit the required package from our interceptor with the log Interception Function!
^ Action:. *, ^ method :.*
Input, back, cancel, browse
Input, back, cancel, browse