SpringBoot學習筆記(三)- 資源訪問

1.靜態資源
(1)靜態資源包括: js、css、 圖片
(2)靜態資源存放在:src/main/resources目錄下面
(3)有時創建的maven目錄並沒有src/main/resources,參考地址:
(不用創建test下的resources,也不用走libraries的步驟)
https://zhidao.baidu.com/question/2077185130180927028.html
(4)靜態資源存放目錄不同,訪問方式也不同
a. 在src/main/resources下直接存放:找不到資源(404)
b. resources下創建文件夾static,訪問:http://localhost:8080/ss1.png
c. resources/static下分類,創建picture,訪問:/picture/ss1.png

2.SpringBoot整合JSP視圖層
(1)更改打包方式:
pom.xml>>>>(第一次忘了改war,還是能訪問到)
<packaging>jar</packaging>==><packaging>war</packaging>
右鍵項目>maven>update project
(2)引入外部tomcat支持
pom.xml>>>>

	<!-- SpringBoot 外部tomcat支持 -->
	<dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>

(3)resources目錄下創建application.properties文件,配置:
spring.mvc.view.prefix=/WEB-INF/JSP/
spring.mvc.view.suffix=.jsp
(4)創建與resources同級的文件夾webapp:
繼續創建webapp/WEB-INF/JSP/helloJSP.jsp
若pom.xml的parent標籤報錯,WEB-INF下創建一個空的web.xml
(5)創建訪問接口helloJSP:

	@Controller //不需返回json格式,不用@RestController
	public class HelloJSP {
		@RequestMapping("/helloJSP")
		public String helloJSP(){
			return "hellojsp";
		}
	}

地址:localhost:8080/helloJSP
3.SpringBoot整合freemarker視圖層
(1)SpringBoot本身是沒有封裝jsp相關依賴的,所以需要外部tomcat支持,需要配置application.properties文件,還需要手動添加webapp文件夾
(2)但是SpringBoot整合了freemarker的依賴:
a. 添加依賴

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

b. resources目錄下創建文件夾templates,創建*.ftl文件
c. 創建訪問接口,與jsp的一致

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章