1:添加拦截器
import org.apache.commons.lang.time.StopWatch;
import com.njhh.fmcp.log.PortalRunLog;
import com.njhh.fmcp.util.BeanMaker;
import com.njhh.fmcp.util.SystemConfigUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* action 拦截器
* @author
*
*/
publicclass ActionInterceptorimplements Interceptor{
/**
*
*/
privatestaticfinallongserialVersionUID = 1L;
private PortalRunLoglog = (PortalRunLog)BeanMaker.getBean("monitorLog");
@Override
public String intercept(ActionInvocation invocation)throws Exception {
//监控的类名
String className =invocation.getAction().getClass().getSimpleName();
//监控的方法名
String methodName = className +"." +invocation.getProxy().getMethod();
StopWatch watch = new StopWatch();
watch.start();
String result = invocation.invoke();
watch.stop();
if (watch.getTime() > SystemConfigUtil.getPageTimeThresholdKey()){
log.info("action执行时间:" + watch.getTime() + " ms [" + methodName +"]");
}
//System.out.println("action执行时间:" + watch.getTime() + " ms[" + methodName + "]");
return result;
}
@Override
publicvoid destroy() {
}
@Override
publicvoid init() {
}
}
2:修改strust.xml
<!-- 默认的配置包 -->
<packagename="defaultPackage"extends="struts-default,json-default,jfreechart-default,spring-default">
<!--配置拦截器 -->
<interceptors>
<interceptorname="actionInterceptor"class="com.njhh.fmcp.interceptor.ActionInterceptor"></interceptor>
<!--自定义拦截链 -->
<interceptor-stackname="actionStack">
<!--执行顺序与配置顺序有关 -->
<interceptor-refname="actionInterceptor"></interceptor-ref>
<interceptor-refname="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!--设置默认拦截链,一个package下只能配置一个,应用所有的没有配置拦截器或拦截链的Action -->
<default-interceptor-ref name="actionStack"></default-interceptor-ref>
<global-results>
<!--公用返回页面 -->
<resultname="success">/jsp/noticeMessage/success.jsp</result>
<resultname="error">/jsp/noticeMessage/error.jsp</result>
<resultname="login"type="dispatcher">/login.jsp</result>
<resultname="defaultLogin"type="redirect">/quitSys.jsp</result>
<resultname="loginOut"type="redirect">/loginAction!loginPage.action
</result>
<resultname="ajaxJson"type="json">
<paramname="contentType">text/html</param>
<paramname="root">ajax_json</param>
</result>
<!--异常返回页面 -->
<resultname="actionException"type="dispatcher">/jsp/comm/error.jsp
</result>
<resultname="serviceException"type="dispatcher">/jsp/comm/error.jsp
</result>
<resultname="daoException"type="dispatcher">/jsp/comm/error.jsp
</result>
<resultname="exception"type="dispatcher">/jsp/comm/error.jsp
</result>
</global-results>
<!--异常处理 -->
<global-exception-mappings>
<!-- Action层异常处理 -->
<exception-mappingresult="actionException"
exception="system.exception.ActionException"/>
<!-- Service层异常处理 -->
<exception-mappingresult="serviceException"
exception="system.exception.ServiceException"/>
<!-- DAO层异常处理 -->
<exception-mappingresult="daoException"
exception="system.exception.DaoException"/>
<!--未知的系统异常,后台没有对此信息进行归类 -->
<exception-mappingresult="exception"exception="java.lang.Exception"/>
</global-exception-mappings>
</package>