Struts2攔截器獲取session

package app.one.action;


import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


public class CheckLogin extends AbstractInterceptor {


/**

*/
private static final long serialVersionUID = 1L;


@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
HttpSession session = ServletActionContext.getRequest().getSession();
Object name = session.getAttribute("name");
String forword = "";
if ("".equals(name) || name == null) {
forword = "index";
System.out.println("index");
} else {
System.out.println("invoke");
invocation.invoke();
}
return forword;


}


}


struts.xml

<interceptors>
        <interceptor name="CheckLogin" class="app.one.action.CheckLogin"></interceptor>
        <interceptor-stack name="CheckLogin">
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="CheckLogin"></interceptor-ref>
        </interceptor-stack>
    </interceptors>
    
    <default-interceptor-ref name="CheckLogin"></default-interceptor-ref>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章