在struts.xml中添加如下配置:
<!-- 配置全局攔截器 -->
<package name="all" extends="struts-default">
<interceptors>
<!-- 定義權限控制攔截器 -->
<interceptor name="authority"
class="akai.cost.ms.base.AuthInterceptor" />
<!-- 定義一個包含權限控制的攔截器棧 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<!-- 定義預設攔截器 -->
<default-interceptor-ref name="mydefault" />
<!-- 定義全局處理結果 -->
<global-results>
<!-- 邏輯名為login的結果,映射到/login.jsp頁面 -->
<result name="login">/login.jsp</result>
</global-results>
</package>
使用方法:其他包繼承這個包名就可以了
<package name="abc" extends="all" namespace="/">
附:攔截器類
package akai.cost.ms.base;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession();
String userName = (String)session.getAttribute("System_UserName");
if(userName == "" || userName == null){//錯誤,回到登入界面
return Action.LOGIN;
}else{
return invocation.invoke();
}
}
}