通過《一.spring boot初始化專案》我們已經會初始化spring boot專案,那本篇文章就說明下初始化專案的具體內容,并撰寫第一個Hello頁面,
-
專案結構

-
mvnw、mvnw.cmd:Maven包裝器腳本,實作本機不安裝Maven,也可以構建專案
-
pom.xml檔案:相信使用maven的同學們應該都知道這個檔案的重要性,主要用于引入依賴,本文用例是web專案,所以初始化專案時,添加的依賴是web和thymeleaf,具體如下
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--POM使用的物件模型版本,強制性的,一般很少改變--> <modelVersion>4.0.0</modelVersion> ? <!--spring boot專案資訊--> <groupId>com.kinglead</groupId> <artifactId>spring-init-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-init-demo</name> <description>Demo project for Spring Boot</description> <packaging>jar</packaging> <!--打包方式,默認是jar--> ? <!--spring boot資訊--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <!--spring boot版本號--> <relativePath/> <!-- lookup parent from repository --> </parent> ? <!--版本號維護--> <properties> <java.version>1.8</java.version> </properties> ? <!--依賴資訊--> <!--spring-boot-starter-xxx是對某一功能的依賴包的集合 用于解決3大問題:1.簡化依賴配置,讓依賴更易管理 2.針對某一功能統一管理,方便依賴引入 3.解決依賴版本沖突問題 --> <dependencies> <!--web專案依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--thymeleaf依賴包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> ? <!--插件--> <build> <plugins> <!--spring boot maven插件 作用:1.允許使用maven允許應用 2.將所有依賴打入到jar包中 3.在jar包中生成manifest檔案,指明引導類 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
SpringInitDemoApplication:spring boot專案啟動類
package com.kinglead.demo; ? import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; ? //@SpringBootApplication注解代表這是一個spring boot應用 //它是一個組合注解 //@SpringBootConfiguration注解將該類宣告為配置類,相當于@Configuration的特殊形式 //@EnableAutoConfiguration啟動spring boot的自動配置 //@ComponentScan啟動組件掃描:將通過@Component、@Controller、@Service這樣注解的類,注冊為spring應用背景關系的組件 @SpringBootApplication public class SpringInitDemoApplication { ? /** * @param args 命令列引數 */ public static void main(String[] args) { SpringApplication.run(SpringInitDemoApplication.class, args); } ? }
-
resources:資源檔案
static:靜態檔案目錄,如image、css
templates:模板檔案,如html
application.properties:組態檔(開始是空的)
-
SpringInitDemoApplicationTests:測驗類
package com.kinglead.demo; ? import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; ? @SpringBootTest class SpringInitDemoApplicationTests { ? @Test void contextLoads() { } ? }
-
-
撰寫控制器(Controller)

package com.kinglead.demo.controller; ? import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; ? /** * @author kinglead * @date 2020-09-08 下午 15:16 * @describe 歡迎頁控制器 */ ? //@Controller標記類為控制器,能讓spring自動掃描到它,添加到容器中 //另外@RestController = @Controller + @ResponseBody,@ResponseBody標記回傳報文是json格式 //@RequestMapping是請求url路徑映射 @Controller @RequestMapping("/") public class HelloController { ? //@GetMapping get請求方法路徑映射 @GetMapping("/") public String hello(){ return "hello"; //回傳的string值,將會被決議為視圖的邏輯名 } ? }
-
撰寫歡迎頁(Hello.html)

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymelead.org"> <head> <meta charset="UTF-8"> <title>Spring Demo</title> </head> <body> <h1>Welcome to Spring</h1> <img th:src="@{/images/1.png}"> </body> </html>
-
啟動應用
如果是使用IDEA開發,直接在啟動類上Run或在工具類點擊Run


下面是啟動日志

-
測驗請求

網頁正常顯示hello歡迎界面,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/346.html
標籤:Java
