Jfinal 阻止重复提交表单

实现功能:阻止用户重复提交表单数据

下载jar包


<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.10.6</version>
</dependency>

  • 1.添加Intercetpor 拦截请求判断是否重复提交

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;

public class RepeatIntercetpor implements Interceptor {

	private final static long timeOut = 2000;

	@Override
	public void intercept(Invocation inv) {
		// 从session中获取user信息
		String user = inv.getController().getSessionAttr("user");
		String className = inv.getController().getClass().getName();
		String methodName = inv.getMethodName();
		String key = user + "." + className + "." + methodName;
		
		// 从缓存中取出当前user调用当前方法的前次时间
		Object o = CacheKit.get("repeat", key);
		long now = System.currentTimeMillis();
		
		// 如果未调用过或是时间差超过timeOut 则继续执行,否则提醒用户不要重复提交
		if (o != null && now - Long.parseLong(o.toString()) <= timeOut) {
			inv.getController().renderJson(new Record().set("message", "请不要重复提交"));
			return;
		} else {
			CacheKit.put("repeat", key, now);
			inv.invoke();
		}
	}
}


  • 2.config中添加EhCachePlugin

EhCachePlugin是JFinal集成的缓存插件,使用EhCachePlugin可以提高系统的并发访问速度。


public void configPlugin(Plugins me) {

		me.add(new EhCachePlugin());
	}

  • 3.配置ehcache.xml,如果没有在src根目录创建文件

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="false" monitoring="autodetect"
         dynamicConfig="true">
         
    <diskStore path="java.io.tmpdir"/>

 	
 	<!-- 
		缓存配置 
		   name:缓存名称。 
		   maxElementsInMemory:缓存最大个数。 
		   eternal:对象是否永久有效,一但设置了,timeout将不起作用。 
		   timeToIdleSeconds:当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空;即缓存被创建后,最后一次访问时间到缓存失效之时,两者之间的间隔,单位为秒(s)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 
		   timeToLiveSeconds:当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清除;即缓存自创建日期起能够存活的最长时间,单位为秒(s)。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 
		   overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 
		   diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 
		   maxElementsOnDisk:硬盘最大缓存个数。 
		   diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. 
		   diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 
		   memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 
		   clearOnFlush:内存数量最大时是否清除。 

	-->

    <cache name="repeat"
           maxEntriesLocalHeap="90000"
           maxEntriesLocalDisk="1000"
           eternal="false"
           overflowToDisk="false"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="300"
           timeToLiveSeconds="100"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off"
            />
              
</ehcache>


  • 4.在需要拦截的方法前添加@Before进行拦截

        @Before(RepeatIntercetpor.class)
	public void method(){
	}

posted @ 2021-06-22 17:11  可爱的黑精灵  阅读(274)  评论(0)    收藏  举报