部署目录如下:
1.在WebMvcConfigurer 配置静态资源映射地址
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //允许访问静态资源,支持classpath:/static/ 及 jar的同级目录/static下 String fileDir = "file:"+System.getProperties().getProperty("user.dir")+"/static/"; registry.addResourceHandler("/**").addResourceLocations("classpath:/static/", fileDir); }
2.配置 thymeleaf 映射路径
默认为 resources,改为 file:${user.dir}/static/
spring: thymeleaf: mode: HTML encoding: utf-8 cache: false prefix: file:${user.dir}/static/
3. controller中写访问地址
/** * 进入页面 index.html --> 访问 localhost:8080/; localhost:8080/index * @return */ @GetMapping({"/", "/index"}) public ModelAndView index() { return new ModelAndView("index.html"); }
4. 后端 jar分离部署,将springboot项目依赖第三方jar放在 /lib 下
我使用的是gradle,build.gradle 配置如下
plugins { id 'java' } group 'com.geline.cloud' repositories { mavenCentral() } dependencies { implementation 'cn.hutool:hutool-all:5.7.6' implementation 'org.springframework.boot:spring-boot-starter-logging' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-aop' } task clearJar(type: Delete) { delete "$buildDir\\libs\\lib" } task copyJar(type: Copy, dependsOn: 'clearJar') { from configurations.compileClasspath into "$buildDir\\libs\\lib" } bootJar.enabled = true bootJar { excludes = ["*.jar"] dependsOn clearJar dependsOn copyJar manifest { attributes "Manifest-Version": 1.0, 'Class-Path': configurations.compileClasspath.files.collect { "lib/$it.name" }.join(' ') } } jar { enabled(true) from('src/main/java'){ include '**/*.xml' } } publishing { repositories { maven { name = 'localRepo' url = "file://${buildDir}/repo" } } publications { myApp(MavenPublication) { from components.java } } }