码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-06-16 13:29:08      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:his   rri   load   deb   stat   string   override   ext   str   

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

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);
	}
}

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


		<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:.*</param>
				</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</param>
				</interceptor-ref>
				<interceptor-ref name="workflow">
					<param name="excludeMethods">input,back,cancel,browse</param>
				</interceptor-ref>
				<interceptor-ref name="debugging" />
				<interceptor-ref name="deprecation" />
			</interceptor-stack>
		</interceptors>

		<default-interceptor-ref name="defaultStack"></default-interceptor-ref>
	</package>


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

标签:his   rri   load   deb   stat   string   override   ext   str   

原文地址:http://www.cnblogs.com/tlnshuju/p/7026926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!