web入門
@RestController 註解
@RestController 相當於 @Controller+@ResponseBody
如果要返回頁面就用ModelAndView
這樣就會被解析成是返回頁面
返回JSON
返回字符串
@RequestMapping("/str")
public String json1(){
return "這是一個字符串";
}
返回對象
@RequestMapping("/obj")
public Person json2(){
return new Person(1L,"liyiyi",new Date());
}
注意 : 對於對象中的日期格式
需要在get方法上 加上@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
返回一個數組
@RequestMapping("/array")
public List<Person> json3(){
return Arrays.asList(new Person(1L,"liyiyi",new Date())
,new Person(2L,"lierer",new Date()));
}
返回頁面
常用到的模板技術
JSP、Velocity、Freemarker、Thymeleaf
SpringBoot推薦的Thymeleaf
語法更簡單,功能更強大
Thymeleaf入門
第一步 導包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
然後我們需要切換Thymeleaf的版本
<properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 佈局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
Thymeleaf的使用
模板存放的默認位置
只要我們把HTML頁面放在classpath:/templates/,thymeleaf就能自動渲染
傳統方式 (@Controller註解)
@RequestMapping("/jsp")
public String jsp(Model model){
//會到classpath:/templates/hello.html
model.addAttribute("msg","hello liyiyi");
return "hello";
}
@RestController註解方式
@RequestMapping("/jsp")
public ModelAndView jsp(){
//會到classpath:/templates/hello.html
return new ModelAndView("hello","msg","hello liyiyi");
}
注意!!!
在HTML頁面需要引入<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf入門</title>
</head>
<body>
<!--jquery juquery.js-->
<h1>成功!</h1>
<!--使用語法th:text 將div裏面的文本內容設置爲 -->
<div th:text="${msg}">這是顯示歡迎信息</div>
</body>
</html>
輸出頁面:
Thymeleaf語法:
正常我們做項目我們都是前後端分離 所以一般不會用這種方式
Springmvc的一些配置
靜態資源配置
靜態資源文件夾映射
配置歡迎頁映射
網頁標題的圖標配置
說明:
- 所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找資源
webjars:以jar包的方式引入靜態資源
http://localhost:8080/webjars/jquery/3.3.1/dist/jquery.js 在訪問的時候只需要寫webjars下面資源的名稱即可
就可以在頁面中引用當前配置<dependency> <groupId>org.webjars.bower</groupId> <artifactId>jquery</artifactId> <version>3.3.1</version> </dependency>
<script th:src="@{/webjars/jquery/3.3.1/dist/jquery.js}"></script>
- “/**” 訪問當前項目的任何資源,都去(靜態資源的文件夾)找映射
- 歡迎頁; 靜態資源文件夾下的所有index.html頁面;被"/**"映射
localhost:8080/
找index頁面 - 所有的 **/favicon.ico 都是在靜態資源文件下找
擴展springmvc配置
如果一個方法裏面沒有任何的邏輯代碼 那麼就可以將
@RequestMapper(“/hello”)
public String success(){
return “success”
}
這塊代碼換成配置中的
<mvc:view-controller path="/hello" view-name="success"/>
這句代碼
而上面這些代碼就可以通過擴展springmvc配置來進行一個操作
編寫一個配置類
addViewController("/hello")
:URL地址
setViewName("hello")
返回的模型視圖
這也就相當於是上面的xml配置
還有就是springMVC中的攔截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>
現在在配置類中就能配置成
addInterceptor(logingInterceptor)
:加入一個攔截器類
addPathPatterns("/**")
:攔截的路徑
excludePathPatterns("/login")
:不攔截的 放行的路徑
定義一個攔截器類 去實現 HandlerInterceptor
並且去覆寫裏面的方法
如果不需要進行攔截 將這個位置的false改爲true
接管SpringMVC
當在配置類中加上@EnableWebMvc這個註解是 就代表這個配置類接管了原來的自動配置 原來的所有自動配置都不能再用了