010 - Interceptor

本文介绍Struts2中自定义拦截器的具体实现方法及如何使用token拦截器防止表单重复提交。通过示例展示了MyInterceptor类的工作流程,并在struts.xml中配置了拦截器的应用。同时,还介绍了如何通过表单元素集成token来增强应用的安全性。

1. 自定义Interceptor

//MyInterceptor.java

public class MyInterceptor extends AbstractInterceptor {

 

 public String intercept(ActionInvocation invocation) throws Exception {


  long start = System.currentTimeMillis();       //Action前做点事


  String r = invocation.invoke();                     //调用Action中的某个方法


  long end = System.currentTimeMillis();        //Action 执行后再做点事


  System.out.println("action time = " + (end - start));
  return r;


 }

}

 

 

 

//struts.xml

<struts>


 <constant name="struts.devMode" value="true"></constant>
 <package name="test" namespace="/" extends="struts-default">
          <interceptors>
                   <interceptor name="my" class="MyInterceptor"/>  //向struts2注册自己的Interceptor
          </interceptors>


          <action name="test" class="TestAction">
                    <result>/test.jsp</result>

                    <!-- 注意两个interceptor-ref的顺序, 顺序不同, 执行效果也不同: 先配置的先执行/后配置的先退出(先进后出) --> 
                     <interceptor-ref name="my"></interceptor-ref>                    //应用到这个Action上
                     <interceptor-ref name="defaultStack"></interceptor-ref>


           </action>

 </package>


</struts>

 

 

2. 使用token拦截器防止重复提交

 //struts.xml

<struts>

     <constant name="struts.devMode" value="true"></constant>
     <package name="test" namespace="/" extends="struts-default">
  
     <action name="input" class="InputAction">
              <result>/input.jsp</result>
     </action>

 

     <action name="user" class="UserAction">
             <result>/addOK.jsp</result>
      
            <interceptor-ref name="defaultStack"></interceptor-ref>


            <interceptor-ref name="token"></interceptor-ref>
            <result name="invalid.token">/error.jsp</result>


     </action>

 </package>

</struts>

 

 

//表单的写法

    <form action="user" method="post">
            name:<input name="name">
            age:<input name="age">
            <input type="submit" value="add">

            <s:token></s:token>
    </form>

String[] extension = file.getFileName().toString().split("."); LogUtil.info(" -------------------------------------------------------------- :" + file.getFileName().toString()); LogUtil.info(" -------------------------------------------------------------- :" + extension[0]); 2025/11/04 11:39:54:685 INFO OLT B_BX_OLT03_010 01 -------------------------------------------------------------- :00000001_1_1.dat 2025/11/04 11:39:54:693 ERROR OLT B_BX_OLT03_010 01 異常終了 jp.co.family.bj.service.B_BX_OLT03_010.B_BX_OLT03_010Service.executeServiceで予期しない例外が発生しました。 jp.co.family.bj.common.exception.ServiceRuntimeException: jp.co.family.bj.service.B_BX_OLT03_010.B_BX_OLT03_010Service.executeServiceで予期しない例外が発生しました。 at jp.co.family.bj.common.aop.ServiceThrowsAdvice.afterThrowing(ServiceThrowsAdvice.java:53) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invokeHandlerMethod(ThrowsAdviceInterceptor.java:179) at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:142) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:727) at jp.co.family.bj.service.B_BX_OLT03_010.B_BX_OLT03_010Service$$SpringCGLIB$$0.executeService(<generated>) at jp.co.family.bj.common.core.TransactionCtrlImpl.execute(TransactionCtrlImpl.java:44) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:359) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:380) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:727) at jp.co.family.bj.common.core.TransactionCtrlImpl$$SpringCGLIB$$0.execute(<generated>) at jp.co.family.bj.common.core.Main.execute(Main.java:108) at jp.co.family.bj.common.core.Main.main(Main.java:60) Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at jp.co.family.bj.service.B_BX_OLT03_010.B_BX_OLT03_010Service.lambda$hsnTaisyoJca$2(B_BX_OLT03_010Service.java:330) at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:178) at java.base/java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:400) at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:261) at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:528) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:647) at jp.co.family.bj.service.B_BX_OLT03_010.B_BX_OLT03_010Service.hsnTaisyoJca(B_BX_OLT03_010Service.java:339) at jp.co.family.bj.service.B_BX_OLT03_010.B_BX_OLT03_010Service.execute(B_BX_OLT03_010Service.java:250) at jp.co.family.bj.common.core.AbstractService.executeService(AbstractService.java:53) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:359) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:137) ... 18 more
11-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值