Struts2 拦截器 及如何获得 servlet 请求对象 以及Struts 基本配置 &&Session 超时设置

在拦截器中可以三种实现

一:继承 AbstractInterceptor 类
二:继承 MethodFilterInterceptor类
三:实现 Interceptor 接口

在实现Interceptor接口时,会多出init() 和 destroy() 方法

拦截器的主体方法是:
public String intercept(ActionInvocation arg0) throws Exception {
// TODO Auto-generated method stub

HttpServletRequest request = ServletActionContext.getRequest();
if(request.getParameter("username") == null){//输入的用户名为空
return "logFail";
}
logInterceptor.invoke();
return null;


}

在拦截器完成任务后-->记得 arg0.invoke()  
激活action,让程序得以继续执行

另外:
<form action="logAction_login.action" method="post">
在struts.xml中<action name="logAction_*" class="my.test.Control" method="{1}"></action>
指定跳转的处理类的处理方法

付一张必须jar包图:

 通过非IOC 方式 获取 http请求参数
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletRespons respons = ServletActionContext.getRespons();
HttpSession session  = ServletActionContext.getRequest().getSession();//servlet 中的底层session
Map sessionMap = ActionContext.getContext().getSession(); //struts2 中封装的Map session

 

------------------------------------------------------------------------------------------>

<struts>
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.objectFactory.spring.autoWire" value="name" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.custom.i18n.resources" value="messages,department,serch,device,login,user,role,ywtj,param,notice,hmd,jrs,jwjk"/>
<include file="struts-login.xml" />

 

</struts>

----------------------------------------------------------------------------------------> 

 判断Session 是否超时:

public class SessionInterceptor extends BaseAction implements Interceptor    {


private static final long serialVersionUID = 1L;
public String intercept(ActionInvocation actionInvocation) throws Exception {
HttpSession session  = ServletActionContext.getRequest().getSession();
Action action = (Action) actionInvocation.getAction();
if (action instanceof LoginAction) {
return actionInvocation.invoke();
}else{
SysUser user = UserSessionUtil.getUserInfo(session);
if (user == null) {
return "login";
} else {
return actionInvocation.invoke();
}
}
}
public void destroy() {
// TODO Auto-generated method stub
}
public void init() {
// TODO Auto-generated method stub
}
}

 ---------------------------------------------------struts.xml 配置:

<package name="global" namespace="/" extends="struts-default">
     <!-- struts2 拦截器Session 超时 -->
   <interceptors>
<interceptor name="sessionInterceptor" class="com.egintra.common.base.SessionInterceptor" />
<!-- 拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="sessionInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
    <default-interceptor-ref name="myStack" />
    
        <global-results>
            <result name="exception">/jsp/error.jsp</result>   
            <result name="login">/jsp/system/login/login.jsp</result>      
        </global-results>
        <global-exception-mappings>
            <exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
        </global-exception-mappings>                
    </package>

 

 

 

posted @ 2013-03-01 17:28  roscee  阅读(389)  评论(0编辑  收藏  举报