package com.djoker.struts2; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class myLogInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("記錄日誌開始"); String recode = invocation.invoke(); System.out.println("記錄日誌結束"); return recode; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="user" extends="struts-default" namespace="/user"> <interceptors> <!-- 聲明過濾器 --> <interceptor name="mylog" class="com.djoker.struts2.myLogInterceptor"></interceptor> <!-- 使用站方式聲明過濾器 --> <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="mylog"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 默認攔截器,若是沒有指定則使用該攔截器 --> <default-interceptor-ref name="myStack"></default-interceptor-ref> <global-exception-mappings> <exception-mapping result="error" exception="com.djoker.struts2.UserNotFoundException"></exception-mapping> </global-exception-mappings> <action name="*User" class="com.djoker.struts2.UserAction" method="{1}User"> <!-- 普通配置,可是每一個Action都須要配置 <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="mylog"></interceptor-ref> --> <!-- <interceptor-ref name="mylog"></interceptor-ref> --> <exception-mapping result="error" exception="com.djoker.struts2.UserNotFoundException"></exception-mapping> <result>/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>