This article brings you an introduction to the relevant operations of the Struts interceptor (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
How to implement aop
Used to implement action before and after execution
Generally used for transaction operations.
Generally used for certain unauthorized operations When the page is accessed, interception operations are performed to intercept illegal access.
Out-of-the-box interceptor
<!-- 拦截器 --> <interceptor-ref name="params"/> <!-- 传递属性拦截器 --> <interceptor-ref name="timer"/> <!-- 测算执行时间 -->
Must have passing attributes for this interceptor
The running output log is as follows
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.
You can see that this action takes 12ms to run in total
Custom interceptor
You need to inherit this abstract class and implement its method. com.opensymphony.xwork2.interceptor.AbstractInterceptor abstract class
The code is as follows
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; } }
Implement the interceptor method of this class
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; } }
Change the configuration file
<!-- 拦截器 --> <interceptor-ref name="params"/> <!-- 传递属性拦截器 --> <interceptor-ref name="timer"/> <!-- 测算执行时间 -->/HelloWorld.jsp /error.html
Effect As follows
The console output is as follows
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.
It can be seen that since 3 interceptors are configured, the interceptors are executed sequentially.
Two operations are performed before and after execution
This is aspect-oriented programming
You can add transaction locks here. The locks can be implemented with redis. When multiple tomcat instances read the database at the same time, the locking operation is performed and redis is used to achieve the purpose of locking
This article has ended here. For more other exciting content, you can pay attention to the Java Video Tutorial column on the PHP Chinese website!
The above is the detailed content of Introduction to related operations of Struts interceptor (with code). For more information, please follow other related articles on the PHP Chinese website!