攔截器的配置
<constant name="struts.action.extension" value="action,,jspx,"/>
<include file="struts-product.xml"/>
<include file="struts-user.xml"/>
<package name="basePackage" extends="struts-default">
<interceptors>
<interceptor name="myTimer" class="com.xixingit.interceptor.TimerInterceptor"></interceptor>
<interceptor name="checkUser" class="com.xixingit.interceptor.CheckUserInterceptor">
</interceptor>
<interceptor-stack name="myInterceptor">
<interceptor-ref name="myTimer"></interceptor-ref>
<interceptor-ref name="checkUser"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 下面這 一行主要是必須要寫的,不寫的情況下會導致攔截器無法使用-->
<default-interceptor-ref name="myInterceptor"/>
<global-results>
<result name = "loginerror" type="redirectAction">
<param name="namespace">/user</param>
<param name="actionName">loginForm</param>
<param name="code">001</param>
</result>
</global-results>
</package>
</struts>
<struts>
<package name="userPackage" extends = "basePackage" namespace="/user">
<action name="loginForm" class = "com.xixingit.action.UserAction" method = "loginForm">
<result>
/WEB-INF/views/user/login.jsp
</result>
</action>
<action name="tologin" class = "com.xixingit.action.UserAction" method = "login">
<!-- <interceptor-ref name="myInterceptor"></interceptor-ref> -->
<result type = "redirectAction">
<param name="namespace">/product</param>
<param name="actionName">index</param>
</result>
<result name = "error">
/WEB-INF/views/user/login.jsp
</result>
<result name="input">
/WEB-INF/views/user/login.jsp
</result>
</action>
</package>
</struts>
重寫的攔截器的類使用者驗證
public class CheckUserInterceptor extends AbstractInterceptor{
public CheckUserInterceptor(){
System.out.println("create.....");
}
private static final long serialVersionUID = 1L;
private String userName;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext actionContext = invocation.getInvocationContext();
ActionProxy ap = invocation.getProxy();
String namespace = ap.getNamespace();
String actionName = ap.getActionName();
if("/user".equals(namespace)&&("loginForm".equals(actionName)||"tologin".equals(actionName))){
return invocation.invoke();
}else{
Map<String,Object> session = actionContext.getSession();
User currUser = (User) session.get("currUser");
if(null != currUser){
return invocation.invoke();
}else{
return "loginerror";
}
}
}
重寫的獲取連結執行完的類
public class TimerInterceptor extends AbstractInterceptor{
private static final long serialVersionUID = 1L;
public String intercept(ActionInvocation invocation) throws Exception {
//獲取當前的時間,類繼承AbstractInterceptor類,其中被繼承的類實現了相對應的介面
long startTime = System.currentTimeMillis();
System.out.println("interceptor....");
//獲取相應的絕對路徑
Object obj = invocation.getAction();
System.out.println("obj:" + obj.getClass().getName());
//這個類中的方法可以進行獲取到名稱空間,方法和actionName
ActionProxy ap = invocation.getProxy();
System.out.println("action:" + ap.getAction());
System.out.println("actionName:" + ap.getActionName());
System.out.println("method:" + ap.getMethod());
System.out.println("namespace:" + ap.getNamespace());
//result是返回一個Success值,用於攔截器的放行
String result = invocation.invoke();
System.out.println("result:" + result);
long endTime = System.currentTimeMillis();
System.out.println( obj.getClass().getName() + "執行" + ap.getMethod()+ "消耗:"+ (endTime - startTime) + "ms");
return result;
}
}