使用token拦截器(自定义拦截器的使用(不重要))

    在页面的提交表单中加入<s:token/>标签,当请求该页面时,由服务器产生一个字符串,如:LO3ZSJ4MGAQH3VMFS425V62PK535THUI可以通过查看ie源码查看生成的字符串。服务器产生这个字符串后将其放入到服务器的session里。同时客户端的字符串将随着提交,token拦截器在action执行之前会比较那个字符串与服务器session的字符串是否一致,比较一次后session中的字符串将会被删除。如果用户重复提交将不会通过。

Form token LO3ZSJ4MGAQH3VMFS425V62PK535THUI does not match the session token null.

这个提示是当用户重复提交时,后台提示的信息。它说的是表达令牌LO3ZSJ4MGAQH3VMFS425V62PK535THUIsession中的空令牌不匹配,说明用户已经重复提交了。当重复提交时,token拦截器会返回一个invalid.token

actionresult里必须配置一个

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

自定义拦截器:

//MyIntercptor.java这个拦截器的作用是计算action执行过程所花费的时间。

package com.xie.interceptor;

 

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

 

@SuppressWarnings("serial")

public class MyInterceptor implements Interceptor{

 

    public void destroy() {    

    }

 

    public void init() {

    }

 

    public String intercept(ActionInvocation invocation) throws Exception {

       long startTime=System.currentTimeMillis();

       //调用invoke

       String r=invocation.invoke();

       long endTime=System.currentTimeMillis();

       System.out.println("time="+(endTime-startTime));

       return r;

    }

//struts.xml中配置:

    <package name="interceptor" namespace="/" extends="struts-default">

     //指定自定义拦截器类

           <interceptors>

                  <interceptor   name="timeinterceptor"                                         class="com.xie.interceptor.MyInterceptor"></interceptor>

           </interceptors>

          

                <default-action-ref name="index" />

                  

                   <action name="index" >

                       <result>/index.jsp</result>

                   </action>

                  

                   <action name="test" class="com.xie.interceptor.InterceptorAction">

                       <result>/test.jsp</result>

                       //使用自定义的拦截器,注意应该加上defaultStack

                       <interceptor-ref name="timeinterceptor"></interceptor-ref>

                       <interceptor-ref name="defaultStack"></interceptor-ref>

                   </action>           

    </package>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值