免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Struts2攔截器和監(jiān)聽器
第一種方法:
直接implements實現(xiàn)com.opensymphony.xwork2.interceptor.Interceptor
Java代碼
public class MyInterceptor implements Interceptor {
public void destroy() {
System.out.println("destroy()");
}
public void init() {
System.out.println("init()");
}
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Interceptor");
String result = invocation.invoke();
return result;
}
}
第二種方法
直接extends com.opensymphony.xwork2.interceptor.AbstractInterceptor
這種方法是少了destroy,init方法
Java代碼
public class MyInterceptor2 extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("MyInterceptor2222");
String result = invocation.invoke();
return result;
}
}
第三種方法
直接extends com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
這種方法能對Action里面的方法級進(jìn)行控制,是否執(zhí)行這個方法
Java代碼
public class MyInterceptor3 extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("use MethodFilterInterceptor");
String result = invocation.invoke();
return result;
}
}
然就是配置
Xml代碼
<interceptors>
<interceptor name="myInterceptor" class="com.langhua.interceptor.MyInterceptor">
</interceptor>
<interceptor name="myInterceptor2" class="com.langhua.interceptor.MyInterceptor2">
</interceptor>
<interceptor name="myInterceptor3" class="com.langhua.interceptor.MyInterceptor3">
<param name="excludeMethods">execute</param>
<param name="includeMethods">addmessage</param>
</interceptor>
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="myInterceptor3"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
里面分interceptor和interceptor-stack
stack也能引用stack
default-interceptor-ref表示Action如果不指定interceptor就用這個default的
extends="struts-default"表示這個XML文件是extends 另一個xml的,名字叫struts-default
在這個XML中,配置了一個常用的interceptor可以去Core包中找到這個XML
interceptor配置很靈活的。如果要寫自己的interceptor就要把<interceptor-ref name="defaultStack"></interceptor-ref>默認(rèn)的給加上,當(dāng)然也可以自己配置。
interceptor的參數(shù)
Xml代碼
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="myInterceptor3"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
這個是因為MethodFilterInterceptor 里有這兩個成員變量,如果你自己的interceptor要帶參數(shù)的話就要相應(yīng)的Action里面寫到成員變量,并加上get,set方法
Java代碼
public abstract class MethodFilterInterceptor extends AbstractInterceptor {
protected transient Logger log = LoggerFactory.getLogger(getClass());
protected Set<String> excludeMethods = Collections.emptySet();
protected Set<String> includeMethods = Collections.emptySet();
監(jiān)聽器
首先要實現(xiàn)com.opensymphony.xwork2.interceptor.PreResultListener類
并重寫里面的方法beforeResult
Java代碼
public class MyListener implements PreResultListener {
public void beforeResult(ActionInvocation invocation, String resultCode) {
System.out.println(resultCode);
}
}
然后再在攔截器里面調(diào)用
Java代碼
invocation.addPreResultListener(new MyListener());
監(jiān)聽器是在這個攔截器完成別的攔截器之后調(diào)用的
struts2 Action獲得HttpSession,HttpServletRequest,HttpSevletResponse的方法
非IOC方式
這種方式主要是利用了com.opensymphony.xwork2.ActionContext類以及org.apache.struts2.ServletActionContext類
Java代碼
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
//ServletActionContext.APPLICATION;
//ServletActionContext.SESSION;
//ServletActionContext.PAGE_CONTEXT;
//或者
HttpServletRequest request = ServletActionContext.getRequest ();
主要是這兩個類com.opensymphony.xwork2.ActionContext和org.apache.struts2.ServletActionContext都對request等進(jìn)行了大量的封裝,直接調(diào)用方法就可以獲和
更好一點的IOC方式
action類實現(xiàn)ServletRequestAware接口,并新建一個HttpServletRequest request
Java代碼
public class UserLoginAction extends ActionSupport implements ServletRequestAware{
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
然后可以生成的request得到對象,如request.getRemoteAddr()
action類實現(xiàn)SessionAware接口,并創(chuàng)建一個MAP對象session
public class UserLoginAction extends ActionSupport implements ServletRequestAware,SessionAware{
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
public void setSession(Map session) {
this.session=session;
}
這些獲得HttpServletRequest等對象需要implments的接口都在
org.apache.struts2.interceptor下面
如Apllication的是ApplicationAware
如HttpSession的是SessionAware(struts2的Session都被封裝成Map了)
如HttpServletRequest的是ServletRequestAware
如HttpServletResponse的是ServletResponseAware
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
struts2學(xué)習(xí)筆記(9)——自定義攔截器
Struts2 攔截器詳細(xì)配置過程
struts2訪問request、session和application對象
登錄過濾器(Struts2)
struts2.x_HttpRequest對象
在struts2中使用攔截器(Interceptor)控制登錄和權(quán)限
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服