SpringMVC中一个超好用的参数缓存框架

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/believer123/article/details/79308573

在上篇博客(http://blog.csdn.net/cml_blog/article/details/78928693)已经分析了页面参数保存的方法以及各种方法。但是是基于AOP的实现方式,既然使用SpringMVC,那么就应该使用框架提供的接口进行使用。现在将SpringMVC的实现方式抽取出来,封装成一个支持自定义拓展的参数缓存框架。已经提交到了maven仓库。
下面对框架进行简单的介绍:

使用场景

  • 需要在跳转其他页面后,返回当前页面还需要参数保持的情况
  • 新增数据需要跳转到预览或确认页面的情况
  • 需要缓存参数的情况
  • 支持的请求方式

GET,POST中支持:application/json,application/x-www-form-urlencoded(表单提交)

如何使用?

  • 添加依赖:
    <dependency>
        <groupId>com.github.cmlbeliever</groupId>
        <artifactId>cacheable-search-mvc</artifactId>
        <version>:lastVersion</version>
    </dependency>

最新从maven仓库获取:http://search.maven.org/#search%7Cga%7C1%7Ccmlbeliever

  • 在Controller中添加注解:@SearchCache
 @RequestMapping("/list")
    public String userList(Model model, @SearchCache() User u) {
        return "user-list";
    }
  • 获取cacheToken

    可以从HttpServletRequest中直接获取到cacheToken,如:
    request.getAttribute(“cacheToken”)

    • 支持的配置

    //自定义缓存实现类的配置
    search-cache.cacheImplRef= 

    //自定义key生成策略的配置
    search-cache.keyGeneratorRef=

    //参数key定义,默认为cacheToken
    search-cache.cacheToken=myToken

    //自定义参数解析,支持CacheableArgumentResolver和HandlerMethodArgumentResolver两种类型
    search-cache.argumentResolvers=
  • 注意:application/json 请求方式时,cacheToken需要放到header中传入,其他请求方式可以直接在参数中传入cacheToken

详见:https://github.com/cmlbeliever/cacheable-search
框架详细的演进说明和总结,详见GitChat:http://gitbook.cn/gitchat/activity/5a52d334ebd9cc598adf1258

猜你喜欢

转载自blog.csdn.net/believer123/article/details/79308573