Struts2 global interceptor that displays request methods and parameters

Source: Internet
Author: User

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
   
   
   
  
 
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.