創建名為springboot_springmvc的新module,過程參考3.1節 4.1、重要的配置參數 在 spring boot 中,提供了許多和 web 相關的配置參數(詳見官方文檔),其中有三個比較重要: 4.1.1、server.port 該配置參數用於設置 web 應用程式的服務埠號 ...
創建名為springboot_springmvc的新module,過程參考3.1節
4.1、重要的配置參數
在 spring boot 中,提供了許多和 web 相關的配置參數(詳見官方文檔),其中有三個比較重要:
4.1.1、server.port
該配置參數用於設置 web 應用程式的服務埠號,預設值為 8080
4.1.2、server.servlet.context-path
該配置參數用於設置 web 應用程式的上下文路徑,預設值為空
4.1.3、spring.resources.static-locations
該配置參數用於設置 web 應用程式的靜態資源(圖片、js、css和html等)的存放目錄(詳見4.2節),
預設值為 classpath:/static 、classpath:/public 、classpath:/resources 和 classpath:/META-INF/resources
4.2、靜態資源目錄的配置
spring boot 定義了靜態資源的預設查找路徑:
classpath:/static 、classpath:/public 、classpath:/resources 和 classpath:/META-INF/resources
只要將靜態資源放在以上的任何一個目錄中(習慣會把靜態資源放在 classpath:/static 目錄下),都能被訪問到。
4.2.1、static靜態目錄示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>static.html</h1>
</body>
</html>
註意:外部訪問靜態資源時,不需要寫預設(或自定義)的靜態資源目錄(本例為 static )
4.2.2、public靜態目錄示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>public.html</h1>
</body>
</html>
註意:外部訪問靜態資源時,不需要寫預設(或自定義)的靜態資源目錄(本例為 public )
4.2.3、resources靜態目錄示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>resources.html</h1>
</body>
</html>
註意:外部訪問靜態資源時,不需要寫預設(或自定義)的靜態資源目錄(本例為 resources )
4.2.4、META-INF/resources靜態目錄示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>META-INF_resources.html</h1>
</body>
</html>
註意:外部訪問靜態資源時,不需要寫預設(或自定義)的靜態資源目錄(本例為 META-INF/resources )
4.2.5、自定義的靜態目錄示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>aaa.html</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>bbb.html</h1>
</body>
</html>
註意:當設置了自定義的靜態資源目錄之後,預設的靜態目錄 classpath:/static 、classpath:/public 、classpath:/resources 失效,
但預設的靜態目錄 classpath:/META-INF/resources 依然有效。
# 設置自定義的靜態資源目錄(本例為 aaa 和 bbb 目錄)
spring.web.resources.static-locations=classpath:/aaa,classpath:/bbb
註意:外部訪問靜態資源時,不需要寫自定義的靜態資源目錄(本例為 aaa )
註意:外部訪問靜態資源時,不需要寫自定義的靜態資源目錄(本例為 bbb )
4.3、自定義攔截器的配置
4.3.1、創建攔截器
package online.liaojy.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author liaojy
* @date 2023/12/20 - 6:48
*/
public class TestInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("TestInterceptor --> preHandle()");
return true;
}
}
4.3.2、創建SpringMVC配置類
SpringMVC配置類的更多內容,請參考SpringMVC教程的14.4節
package online.liaojy.config;
import online.liaojy.interceptor.TestInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author liaojy
* @date 2023/12/20 - 7:04
*/
// 配置類只要放在啟動類所在的包或者子包即可生效
@Configuration
public class SpringMVCConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
TestInterceptor testInterceptor = new TestInterceptor();
registry.addInterceptor(testInterceptor).addPathPatterns("/**");
}
}
4.3.3、測試效果
@RequestMapping("/testInterceptor")
public String testInterceptor(){
System.out.println("TestController --> testInterceptor()");
return "testInterceptor()";
}
本文來自博客園,作者:Javaer1995,轉載請註明原文鏈接:https://www.cnblogs.com/Javaer1995/p/17915296.html