這篇文章帶給大家的內容是關於Struts攔截器的相關操作介紹(附程式碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
實作aop的方式
用於實作action之前,之後執行
一般用於事物操作.
#一般用於對某些未授權的頁面存取的時候,進行攔截操作,攔截非法存取.
開箱即用攔截器
<!-- 拦截器 --> <interceptor-ref name="params"/> <!-- 传递属性拦截器 --> <interceptor-ref name="timer"/> <!-- 测算执行时间 -->
必須有傳遞屬性這個攔截器
運行輸出日誌如下
2019-03-24 03:50:19.231 [DEBUG] com.opensymphony.xwork2.ognl.SecurityMemberAccess.isAccessible(SecurityMemberAccess.java:67) - Checking access for [target: com.ming.HelloWorldAction@33e67d25, member: public java.lang.String com.ming.HelloWorldAction.getName(), property: name] 2019-03-24 03:50:19.232 [INFO ] com.opensymphony.xwork2.interceptor.TimerInterceptor.doLog(TimerInterceptor.java:205) - Executed action [//hello!execute] took 12 ms.
可以看到這個action一共運行耗時12ms
自訂攔截器
需要繼承該抽象類別,並實作其方法即com.opensymphony.xwork2.interceptor.AbstractInterceptor 抽象類別
#程式碼如下
package com.ming; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor extends AbstractInterceptor { /** * Override to handle interception * * @param invocation */ @Override public String intercept(ActionInvocation invocation) throws Exception { return null; } }
實作該類別的interceptor方法
package com.ming; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class MyInterceptor extends AbstractInterceptor { /** * Override to handle interception * * @param invocation */ @Override public String intercept(ActionInvocation invocation) throws Exception { Logger logger = LogManager.getLogger(); // 执行结果前 String output = "hi before"; logger.info(output); // 开始执行Action String result = invocation.invoke(); // 执行结果后 output = "hi after"; logger.info(output); // 继续传递到下一个拦截器 return result; } }
#更改設定檔
<!-- 拦截器 --> <interceptor-ref name="params"/> <!-- 传递属性拦截器 --> <interceptor-ref name="timer"/> <!-- 测算执行时间 -->##效果效果如下控制台輸出結果如下/HelloWorld.jsp /error.html
2019-03-24 04:37:24.086 [DEBUG] com.opensymphony.xwork2.ognl.SecurityMemberAccess.isAccessible(SecurityMemberAccess.java:67) - Checking access for [target: com.ming.HelloWorldAction@5121691d, member: public void com.ming.HelloWorldAction.setName(java.lang.String), property: name] 2019-03-24 04:37:24.087 [INFO ] com.ming.MyInterceptor.intercept(MyInterceptor.java:19) - hi before 2019-03-24 04:37:24.089 [DEBUG] com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:430) - Executing action method = execute 2019-03-24 04:37:24.106 [DEBUG] com.opensymphony.xwork2.ognl.SecurityMemberAccess.isAccessible(SecurityMemberAccess.java:67) - Checking access for [target: com.ming.HelloWorldAction@5121691d, member: public java.lang.String com.ming.HelloWorldAction.execute() throws java.lang.Exception, property: null] 2019-03-24 04:37:24.143 [DEBUG] com.opensymphony.xwork2.ognl.SecurityMemberAccess.isAccessible(SecurityMemberAccess.java:67) - Checking access for [target: org.apache.struts2.result.ServletDispatcherResult@2e4369c6, member: public void org.apache.struts2.result.StrutsResultSupport.setLocation(java.lang.String), property: location] 2019-03-24 04:37:24.143 [DEBUG] com.opensymphony.xwork2.ognl.SecurityMemberAccess.isAccessible(SecurityMemberAccess.java:67) - Checking access for [target: org.apache.struts2.result.ServletDispatcherResult@2e4369c6, member: public void org.apache.struts2.result.StrutsResultSupport.setLocation(java.lang.String), property: location] 2019-03-24 04:37:24.150 [DEBUG] org.apache.struts2.result.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:127) - Forwarding to location: /error.html 2019-03-24 04:37:24.158 [DEBUG] com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.nullPropertyValue(InstantiatingNullHandler.java:98) - Entering nullPropertyValue [target=[com.ming.HelloWorldAction@5121691d, com.opensymphony.xwork2.DefaultTextProvider@2e34626e], property=struts] 2019-03-24 04:37:24.175 [INFO ] com.ming.MyInterceptor.intercept(MyInterceptor.java:26) - hi after 2019-03-24 04:37:24.176 [INFO ] com.opensymphony.xwork2.interceptor.TimerInterceptor.doLog(TimerInterceptor.java:205) - Executed action [//hello!execute] took 89 ms.可以看到,由於配置了3個攔截器,攔截器被順序執行.
在執行前後分別執行了兩個操作
此處可以添加事物鎖 其中鎖可以用redis實現,達到同時多個tomcat實例讀取資料庫的時候,進行加鎖操作,使用redis達到鎖的目的
Java影片教學專欄!
#
以上是Struts攔截器的相關操作介紹(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!