溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-08-20 19:30:29 來源:腳本之家 閱讀:111 作者:hello_dakewang 欄目:編程語言

在application.propertis中配置

##端口號
server.port=8081
##默認前綴
spring.mvc.view.prefix=/
## 響應(yīng)頁面默認后綴
spring.mvc.view.suffix=.html
# 默認值為 /**
spring.mvc.static-path-pattern=/**
# 這里設(shè)置要指向的路徑,多個使用英文逗號隔開,默認值為 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;
/**
 * 手動配置靜態(tài)資源路徑
 * 
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
  @Override
  public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false).
        setUseTrailingSlashMatch(true);
  }
}

在controller中

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

在瀏覽器中訪問地址

localhost:8081/index

以上所述是小編給大家介紹的Spring boot 默認靜態(tài)資源路徑與手動配置訪問路徑的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI