1. 程式人生 > >SpringBoot學習筆記(二):SpringBoot訪問靜態檔案、捕獲全域性異常、整合Thymeleaf、整合JSP

SpringBoot學習筆記(二):SpringBoot訪問靜態檔案、捕獲全域性異常、整合Thymeleaf、整合JSP

SpringBoot訪問靜態檔案

什麼是靜態檔案?
不需要通過web容器去得到的檔案,直接通過路徑就能得到的檔案,比如專案的css,js,img等檔案。
所有的資原始檔都應該在src/main/resources(maven專案中會自動建立這個資料夾)資料夾下面,但在SpringBoot中,系統預設掃描靜態檔案在static或者public資料夾下,這裡我們在src/main/resources目錄下建立一個static資料夾,我們copy一張圖片到static目錄下,將圖片命名為hello.png,我們啟動專案並通過瀏覽器進行訪問這張圖片
例如:
src/main/resources------>static------->hello.png
訪問:localhost:8080/helo.png

SpringBoot採用預設掃描的方式,配置:預設掃描application.properties檔案
同樣是放在src/main/resources下。

SpringBoot捕獲全域性異常

以前捕獲全域性異常需要進行aop封裝,這個功能在SpringBoot中的到了封裝,我們直接用就好了
@ExceptionHandler 表示攔截異常
@ControllerAdvice 是 controller 的一個輔助類,最常用的就是作為全域性異常處理的切面類
@ControllerAdvice 可以指定掃描範圍
@ControllerAdvice 約定了幾種可行的返回值,如果是直接返回 model 類的話,使用 @ResponseBody 進行 json轉換

我們在專案下面新建一個包com.exception,新建一個異常攔截類GlobalExceptionHandler進行攔截Controller丟擲的異常,我們這裡攔截RuntimeException執行時異常,只需要在Controller裡面製造一個異常,在hello請求裡面增加 int a = 10 / 0;即可被捕獲,其他的異常型別大家自己下去演示。
程式碼:

@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public
Map<String, Object> exceptionHandler() { Map<String, Object> map = new HashMap<String, Object>(); map.put("errorCode", "404"); map.put("errorMsg", "找不到頁面了!"); return map; } } }

SpringBoot整合thymleaf

在之前的示例中,我們都是通過@RestController來處理請求,所以返回的內容為json物件。那麼如果需要渲染html頁面的時候,要如何實現呢?
我們說到了SpringBoot是SSM框架和SSH框架的替代品,那麼毫無疑問也是用於搭建Web專案,前面簡單了提到了控制層的使用和異常的捕獲,這裡,我們講一下前臺頁面的展示。
SSM框架使用的JSP作為前臺展示頁面,JSP在執行時將要編譯成一個Servlet,這樣對伺服器是一個大大的消耗。而今天,我們只需要使用前臺模版引擎,這種方式就好比靜態頁面的方式,不需要佔用伺服器太多的資源,這又是我們轉投SpringBoot的有一個不可抗拒的理由。

幾種前臺模版引擎:

Thymeleaf
FreeMarker
Groovy
這裡介紹三種,這些都是SpringBoot官方推薦使用的模版,而且特別說出了避免使用JSP,目前用的最多的就是前兩種,
當我們使用上述模板引擎中的任何一個,SpringBoot預設的模板配置路徑為:src/main/resources/templates。當然也可以修改這個路徑,具體如何修改,可在後續各模板引擎的配置屬性中查詢並修改。所以今天我們需要在src/main/resources目錄下建立一個templates資料夾用來存放我們的前臺模版頁面。
當使用模版的時候我們需要在配置檔案中新增幾條配置,SpringBoot自動會讀取預設的配置檔案路徑是在resources下的application.properties檔案,這裡我們在配置檔案中配置下面的引數

spring.thymeleaf.suffix=.html  
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html

我們使用Thymeleaf的話需要引入相關的jar包

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

這裡我們在前臺頁面目錄templates下新建一個index.html檔案,內容如下

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h2>Welcome To Majiaxueyuan For Study</h2>
</body>
</html>

然後我們在com.controller下新建一個Controller命名為CommonController,要返回到頁面我們和SSM框架一樣,在類上註解Controller而不是RestController,RestController代表是返回JSON字串給前臺,而Controller註解是返回頁面,具體程式碼如下:

@Controller
public class CommonController {
@RequestMapping("/index")
public String index() {
return "index";
}
}

然後這裡就配置完成了我們的基本的頁面和跳轉,我們就可以通過瀏覽器進行訪問了。
到這裡我們訪問127.0.0.1:8080/index就可以跳轉到我們的index.html頁面了。

thymleaf展示資料

@Controller
public class CommonController {
@RequestMapping("/index")
public String index(model) {
List<String> list = new ArrayList<>();
List.add("AAAA");
List.add("bbbbb")
model.addAttribute("list","list")
model.addAttribute("name","我們一起學貓叫")
return "index";
}
}
<input type="text" class="layui-input" th:value="${name}" />
<li th:text="${name}"></li>
<table border="1">  
 <tr>  
   <th>使用者名稱</th>  
   <th>郵箱</th>  
 </tr>  
 <tr  th:each="user : ${userlist}">  
    <td th:text="${user.userName}">Onions</td>  
    <td th:text="${user.email}">[email protected].com.cn</td>
 </tr>
</table>

<table>
		<tr>
			<th>編號</th>
		</tr>
		<tr th:each="l:${list}">
			<td th:text="${l;}"></td>
		</tr>

</table>
<li th:if="${name == null}">為空才顯示</li>
<a th:></a>
<img th:src="@{/img/logo.png}"/>
//包括css等都要加th:    @{}

Thymeleafz=中@符號的作用就是獲取當前專案的路徑

SpringBoot整合JSP(不推薦)

引入jsp的包

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

配置檔案中配置下面的引數

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

這樣的話我們就可以像以前一樣使用JSP進行訪問了