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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2 全局拦截器,显示请求方法和參数相关的知识,希望对你有一定的参考价值。

后台系统中应该须要一个功能那就是将每一个请求的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 全局拦截器,显示请求方法和參数的主要内容,如果未能解决你的问题,请参考以下文章

Struts2自定义拦截器处理全局异常

super()的作用

1Struts2的简单搭建方法

Struts2学习笔记

Struts2中Struts.xml的作用是啥?

servlet请求被struts2拦截器拦截,导致页面访问不到,