SpringBoot應用合並整合前端Vue代碼


1.將前端的vue項目生成的文件放到后端對應目錄

前端文件:

 

 

 

后端目錄結構:

 

 

2.引入thymeleaf依賴

 

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

  

3.更改配置文件

# thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false
spring.resources.static-locations=classpath:/static/
spring.mvc.static-path-pattern=/static/**

4.添加代碼

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("")
public class IndexController {
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}

5.瀏覽器打開

訪問地址為:

http://localhost:8080/index

6.補充

如果想配置多個spring.resources.static-locationsspring.mvc.static-path-pattern

@Configuration
public class MyWebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
        registry.addResourceHandler("/common/**").addResourceLocations("classpath:/common/");
    }
}

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM