在我們開發Web應用的時候,需要引用大量的js、css、圖片等靜態資源。
默認配置
Spring Boot默認提供靜態資源目錄位置需置於classpath下,目錄名需符合如下規則:
/static
/public
/resources
/META-INF/resources
- 舉例:我們可以在src/main/resources/目錄下創建static,在該位置放置一個圖片文件。啟動程序后,嘗試訪問http://localhost:8080/D.jpg。如能顯示圖片,配置成功。
2 .springboot 使用freemarker模板引擎,模板引擎的作用:是為了使用戶界面與業務數據(內容)分離而產生的。
導入freemarker模板引擎所需要的依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jiahou</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 引入springboot 父類依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <!-- 用於web開發的話 導入 springboot -web組件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>*</exclude> </excludes> <filtering>true</filtering> </resource> </resources> </build> </project>
編寫 測試的controller
package com.springboot.hello; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; // 該注解的作用 是表示 在HelloSpringboot類的所有方法 返回的都是json格式 @Controller public class Springbootfmk { @RequestMapping("/index1") public String index(ModelMap map) {// ModelMap轉發值的作用 map.addAttribute("name", "springboot使用freemarker"); return "index1"; }
然后 在src/main/resources目錄下創建templates文件夾 並且 創建一個名字為index1 后綴是.ftl的文本格式:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title></title> </head> <body> ${name} </body> </html>
運行結果:
3.全局異常捕獲,在程序中不能給用戶 返回404 或者500 可以進行統一的處理消息 針對返回json格式
package com.springboot.hello; import java.util.HashMap; import java.util.Map; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; // @ControllerAdvice 是 controller 的一個輔助類,最常用的就是作為全局異常處理的切面類 (可以指定掃描的范圍) @ControllerAdvice public class EexceptionController { // 攔截運行時異常 @ExceptionHandler(RuntimeException.class) @ResponseBody public Map<String, String> disException() { Map<String, String> map = new HashMap<String, String>(); map.put("異常處理", "500"); return map; } }
@RequestMapping("testException") public void testException() { System.out.println(1 / 0); }
運行結果:
4。springboot 一般不推薦使用jsp,不過 要使用jsp 創建springboot的項目的時候 一定要時候war類型。不然會一直找不到頁面,這里不記錄