Java基础、中级、高级、架构面试资料

WebFlux 整合 thymeleaf 教程

JAVA herman 3071浏览
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

WebFlux 是一种趋势,作为程序员我们也应该看到未来的趋势,未来的热门技术。我个人感觉 WebFlux 未来会取代 Spring MVC,因此我花了很多的时间和精力在学习 WebFlux。本文我们一起来学习 WebFlux 整合 thymeleaf 吧!

先看一下,我们的运行效果吧!

WebFlux 整合 thymeleaf 教程

下面我们开始代码的编写。

WebFlux 整合 thymeleaf 也非常的简单,我们只需要在 spring-boot-starter-webflux 的基础上,再引入 spring-boot-starter-thymeleaf 即可。相关的 pom.xml 文件内容如下:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <!-- thymeleaf 模版引擎 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!-- 热部署 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

然后我们在项目的 src/main/resources/templates/ 下新建一个测试的模板页面 xttblog.html。xttblog.html 页面内容你可以自行规划。

然后,我们在 src/main/resources/ 目录下,新建 application.yml 配置文件。内容如下:

spring:
  thymeleaf:
    prefix: classpath:/templates/
    enabled: true
    suffix: .html
    cache: false
    template-resolver-order: 1
    check-template: false
    reactive:
      max-chunk-size: 8192

最后是 Java 代码,为了方便我就贴到一起了。

@Configuration
public class OAuthWeixinRouter {
    @Autowired
    private WeixinHandler weixinHandler;
    @Bean
    public RouterFunction<ServerResponse> webFluxRoutesRegister(){
        return RouterFunctions
                .route(RequestPredicates.GET("/weixin/login"), weixinHandler::loginPage);
    }
}
@Component
public class WeixinHandler {
    public Mono<ServerResponse> loginPage(ServerRequest serverRequest) {
        Map<String, Object> map = new HashMap<>();
        map.put("site", "www.xttblog.com");
        map.put("siteName", "业余草");
        //返回thymeleaf模版页面
        return ServerResponse.ok().render("weixin", map);
    }
}
@SpringBootApplication
public class OAuthWeixinApplication {
    public static void main(String[] args) {
        SpringApplication.run(OAuthWeixinApplication.class, args);
    }
}

Ok,到这里,我们的代码就已经写完了,接下来就启动项目,测试测试吧!thymeleaf 中的所有语法和功能都可以在你的 html 中使用。

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » WebFlux 整合 thymeleaf 教程