了解java 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"/> <!-- 测算执行时间 -->

<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 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拦截器的相关操作 的全部内容, 来源链接: utcz.com/z/342021.html

回到顶部