Spring Boot 引入 thymeleaf

这里以 vue打包的dist为例


第一步,首先将vue项目打包至dist文件夹(若开发者不熟悉vue前端,直接将此项任务交给前端,让他传给你dist)

如图所示,npm run build 是打包命令,打包完成后便会生成一个 dist 文件夹


第二步,在springboot项目 pom.xml文件中引入thymeleaf依赖

        代码如下: 

        <!--依赖web相关模块-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--thymeleaf模板-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
			<version>2.1.6.RELEASE</version>
		</dependency>


第三步,在 resources文件夹下 创建两个文件夹,一个命名为 static,一个命名为 templates。

 static里面放dist文件夹的东西(就是vue打包之后的东西,css和js文件以及html都在里面)(注意static里面放的不是dist文件夹,而是dist里面的那些文件)

        templates里面放的就是初始页面的文件,如我的就是index.html


第四步,配置WebMvcConfig。

        在java下创建config包,并在包内创建WebMvcConfig配置类

         代码如下:


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/#").setViewName("index");
    }

}


第五步,配置application.yml

         代码如下:(注意web配置在spring之下,static-locations配置就是访问静态文件的位置)


spring:
  web:
    resources:
      static-locations: classpath:/static



第六步,启动springboot

       相信这一步所有程序员都会的,这里就不展示了。


第七步,访问页面。

        在第四步中,我配置的访问路径是 /# , springboot端口是8081

        那么我的页面访问路径就是         http://localhost:8081/#

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

稻草人想看远方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值