软件编程
位置:首页>> 软件编程>> java编程>> Spring Boot2.0中SpringWebContext找不到无法使用的解决方法

Spring Boot2.0中SpringWebContext找不到无法使用的解决方法

作者:iquanzhan  发布时间:2023-05-26 02:37:01 

标签:springboot2.0,springwebcontext

前言

为了应对在SpringBoot中的高并发及优化访问速度,我们一般会把页面上的数据查询出来,然后放到redis中进行缓存。减少数据库的压力。

在SpringBoot中一般使用


thymeleafViewResolver.getTemplateEngine().process("goodlist", ctx);

进行页面的渲染,而这个ctx就是SpringWebContext对象,我们一般进行如下获取:


SpringWebContext swc=new SpringWebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap(),applicationContext);

在SpringBoot 1.X的版本中以上代码可以使用。但在SpringBoot 2.0中,就无法找到SpringWebContext了。那应该如何去解决这个问题呢?

解决思路

说一下我的思路,.process方法中ctx所在参数所需要的类型为接口IContext

Spring Boot2.0中SpringWebContext找不到无法使用的解决方法

也就是需要有实现了IContext的类就可以了,然后进入IContext接口找所有的实现类

Spring Boot2.0中SpringWebContext找不到无法使用的解决方法

然后看到WebContext似乎有些像上面所用的SpringWebContext。即做出如下改变,完美实现了thymeleaf的页面渲染。


WebContext ctx = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
html = thymeleafViewResolver.getTemplateEngine().process("goodlist", ctx);

在SpringBoot 2.0中使用上述代码,可以完全替代。

(当然在下不才,暂时只找到了这种办法,在网络上也没找到对应的比较不错的策略。所以分享出来,以备分享出来,帮助遇到此问题的程序员们。如果大家有什么更好的处理办法可以一起互相交流哦)

目前我正在搞基于SpringBoot、Redis、消息队列的秒杀小项目,主要还是为了梳理如何解决高并发的问题过程。

GitHub:https://github.com/iquanzhan/SecKillShop (本地上传)

所用技术

1.后端:SpringBoot、JSR303、MyBatis

2.前端:Thymeleaf、BootStrap、Jquery

3.中间件:RabbitMQ、Redis、Druid

来源:https://juejin.im/post/5c039b2ef265da61682b1e9e

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com