Technology Sharing

Integrate the Vue project into the Springboot project and run it on Alibaba Cloud

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

The first step is to use the thymeleaf template engine in springboot

Importing Dependencies

  1. <!-- thymeleaf 模板 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  5. </dependency>

Create static folder and templates folder in the resources directory

Configure thymeleaf in yml

  1. spring:
  2. # 模板引擎
  3. thymeleaf:
  4. mode: HTML5
  5. encoding: utf-8
  6. # 禁用缓存
  7. cache: false

Enable access to static files in the configuration

  1. public class ResourceConfig implements WebMvcConfigurer {
  2. @Override
  3. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  4. registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
  5. }
  6. }

Package the Vue project

npm run build

Put the static files in the static folder after packaging, and put index.html in the templates folder

Write routes in controller

Let it jump to the index.html page

  1. @Controller
  2. @CrossOrigin
  3. public class IndexController {
  4. @GetMapping("/")
  5. public String index(){
  6. return "index";
  7. }
  8. }

Run the project

Enter the port and access it!