SpringBoot裏面只有src目錄,在src/main/resources下面有兩個文件夾,[static]和[templates],springboot默認static中放靜態頁面,而templates中放動態頁面
themleaf和freemarker的依賴不會產生矛盾,配置這兩個依賴作用是controller返回的視圖解析;
搭配application中配置的jsp的前後綴和themleafcalsspath都會影響返回的指向頁面路徑,所以選擇哪種頁面解析不要輕易更改。看到好多說thymeleaf是官方推薦,其實freemarker纔是,下面基本比較文章中有說明
關於Freemarker和Thymeleaf
框架基本的比較
性能比較
springboot跳轉jsp
- 加入依賴 servlet-api 的 和 tomcat-embed-jasper的如果需要jsp頁面的表達式支持還需要jstl的依賴
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
- 添加正確路徑的webapp,新建自己的文件夾名稱,添加web的設置
添加webapp的文件夾,注意與java,resources平級,
在file–>project stucture中添加web模塊並指明路徑:看是否有web,沒有添加(下圖紅色箭頭添加web),然後在右側右下角web resource Directionies增加+指向該項目的webapp目錄,
然後就可以看到webapp文件夾上有藍點代表成功,在這裏就可以new->jsp;沒有添加web這一步是new中沒有jsp選項的
3. 配置application.yml或者properties 中尋址的前後綴,
在application中添加
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
- controller的編寫
跳轉指向具體的位置,是與配置中的前後綴拼接後是否指向正確路徑
類上的註釋 必須是**@controller**,如果是 @RestController是不能返回頁面的
@GetMapping(value = "/index")
public String indexw() {
System.out.println("====this is index");
return "index";
}
需要注意的一點
這種方法必須用mvn spring-boot:run這種方式啓動纔可以訪問得到(IDEA右側有Maven->plugin中有run的選項)
而直接啓動main方法,看起來是啓動無異常,但是會404找不到具體的頁面
詳細原因,這版講的很清楚
大致原因是不同的啓動方式有不同的calsspath會導致找不到具體的jsp頁面
springboot跳轉html
除了以下的方法通過controller跳轉,也可直接訪問localhost:8080/xxx.html這種也是可以的(但是默認靜態頁面放在static中才有效果跳轉的默認指向templates)
- 增加thymeleaf的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 配置前綴在application中 ,作用爲知名返回的路徑和這個前綴拼接可以找到具體的文件夾,並頁面放在這之下
spring.thymeleaf.prefix=classpath:/templates/
- 配置頁面,新建項目一般已經有了具體文件夾了
- 配置具體的跳轉controller
@RequestMapping("/feng")
public String getHtml(){
System.out.println("====");
return "feng";
}
直接跳轉成功顯示。
springboot跳轉Thymeleaf
- 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 新建them.html 在templates目錄下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>themleaf</title>
</head>
<body>
<p>welcome to <span th:text="${key}"></span>.</p>
</body>
</html>
- controller中添加屬性,return返回頁面,並不需要配置前後綴默認跳轉templates,前後綴如果有需要在application中配置 spring.thymeleaf.prefix=classpath:/templates/
@RequestMapping("/themleaf")
public String themleaf(Model model) {
System.out.println("themleaf----->");
model.addAttribute("key", "三壇海會大神哪吒");
return "them";
}
springboot跳轉FreeMarker
- 增加依賴,去掉tomcat-embed-jasper的依賴,如果同時存在freemaker和jsp的頁面解析,優先返回freemarker
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
- application 中配置端口,其他不需要
- 在templates文件夾下配置頁面,新建chu.ftl模板文件
<html>
<head>
<title></title>
</head>
<body>
hello welcome, ${name}
</body>
</html>
- 配置controller跳轉頁面
@RequestMapping("/home")
public String home1(Model model) {
System.out.println("chu====-");
model.addAttribute("name", "老鐵");
return "chu";
}