Spring boot 默認靜態資源路徑與手動配置訪問路徑的方法


這篇文章主要介紹了Spring boot 默認靜態資源路徑與手動配置訪問路徑的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
 

在application.propertis中配置

##端口號
server.port=8081
##默認前綴
spring.mvc.view.prefix=/
## 響應頁面默認后綴
spring.mvc.view.suffix=.html
# 默認值為 /**
spring.mvc.static-path-pattern=/**
# 這里設置要指向的路徑,多個使用英文逗號隔開,默認值為 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
spring.resources.static-locations= classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/****/

如果自定義訪問路徑則需要添加WebConfig配置類

package com.dakewang.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
 * 手動配置靜態資源路徑
 * 
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
  @Override
  public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false).
        setUseTrailingSlashMatch(true);
  }
}

在controller中

/**
 * 跳轉index.html頁面
 * @return
 */
@RequestMapping("/index")
public String indexHtml() {
  return "index";
}

在瀏覽器中訪問地址

localhost:8081/index


免責聲明!

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



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