龙空技术网

Struts 拦截器

mySoul2018 103

前言:

如今小伙伴们对“apache struts2和apache tomcat”可能比较关心,我们都想要知道一些“apache struts2和apache tomcat”的相关资讯。那么小编在网上汇集了一些关于“apache struts2和apache tomcat””的相关内容,希望朋友们能喜欢,你们快快来学习一下吧!

介绍

实现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"/> <!-- 测算执行时间 --> <interceptor-ref name="MyInterceptor"/> <!-- 成功返回页面 --> <result name="success">/HelloWorld.jsp</result> <result name="error">/error.html</result>

效果如下

控制台输出结果如下

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 before2019-03-24 04:37:24.089 [DEBUG] com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:430) - Executing action method = execute2019-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.html2019-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 after2019-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达到锁的目的

标签: #apache struts2和apache tomcat #apache运行struts