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 是否超時:
?---------------------------------------------------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>?
?
?
轉載于:https://www.cnblogs.com/leonkobe/archive/2013/03/01/2939128.html
總結
以上是生活随笔為你收集整理的Struts2 拦截器 及如何获得 servlet 请求对象 以及Struts 基本配置 Session 超时设置...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android ImageButton设
- 下一篇: getdate()函数的用法.`