在application.propertis中配置html
##端口號 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配置類java
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中web
/** * 跳轉index.html頁面 * @return */ @RequestMapping("/index") public String indexHtml() { return "index"; }
在瀏覽器中訪問地址spring
localhost:8081/index瀏覽器