Spring Boot快速創建Hello World

轉載請註明出處:
https://blog.csdn.net/aa464971/article/details/80399765

Github地址:
https://github.com/dengyuhan/SpringBootSample

相關鏈接

https://projects.spring.io/spring-boot
https://www.thymeleaf.org

介紹

Spring Boot跟Spring MVC不太一樣,Spring MVC建新項目的時候是要配置很多東西的,而Spring Boot講究的是快速,提供了很多默認配置,所以新建一個項目不需要手動配置任何東西,並且個性化配置也比Spring MVC簡單很多。

創建新項目

  • 創建一個新項目,Maven

  • 輸入GroupIdArtifactId

  • 輸入項目名稱並指定項目存放的路徑

引入SpringBoot

打開pom.xml,引入SpringBoot

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

如果提示是否需要開啓自動導入,選擇Enable Auto-Import,否則更改了pom.xml不會自動更新

創建Application

創建SampleApplication,加上註解@SpringBootApplication,這個類的main方法就會成爲整個程序的入口,@EnableAutoConfiguration開啓自動配置,如果有需要,可以通過@ComponentScan再指定自動掃描的包

package com.springboot.sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableAutoConfiguration
@SpringBootApplication
public class SampleApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleApplication.class, args);
    }

}

創建Controller

  • 創建SampleController,並註解@Controller,加上訪問路徑的註解
  • 在SampleController內創建一個home方法,在方法上註解@RequestMapping("/"),這樣就根目錄的訪問路徑註冊到這個方法上,當訪問http://localhost:8080的時候,就會執行這個方法
  • 這裏還註解了@ResponseBody,表示輸出Hello World!字符串
package com.springboot.sample.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SampleController {
    /**
     * ResponseBody樣例
     * @return
     */
    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }
}

運行配置

Run - Run Configurations

+ - ApplicaltionMain-Class選擇最開始創建的SampleApplication
Spring Boot是不需要發佈到外部Tomcat的,所以確定後直接運行即可

測試

運行完成後,在瀏覽器訪問http://localhost:8080,就可以看到SampleControllerhome方法返回的Hello World!

* hymeleaf支持

在Spring Boot中,官方是不推薦使用JSP的,所以我們需要用thymeleaf模板

引入thymeleaf

pom.xml加入依賴

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

創建模板html

resources文件夾下創建templates文件夾,再創建一個index.html

配置訪問路徑

在之前創建的SampleController增加了一個index方法,路徑配置到/index,這裏加入一個message參數傳到頁面,再return "index",這樣當訪問http://localhost:8080/index的時候,就會跳轉到resources/templates文件夾下面的index.html

/**
 * thymeleaf樣例
 * @return
 */
@RequestMapping("/index")
String index(Model model) {
    model.addAttribute("message","Hello Thymeleaf");
    return "index";
}

在模板html獲取參數

index.html中,用thymeleaf語法獲取SampleController添加的message參數。

<p th:text="${message}"></p>

注意還要在html標籤上加上命名空間

xmlns:th="http://www.w3.org/1999/xhtml"

如果你用的是Vue,還需要在script標籤上加上th:inline

<script th:inline="javascript">

測試

在瀏覽器訪問http://localhost:8080/index

*導出可執行jar

在pom.xml指定包類型並添加插件

<packaging>jar</packaging>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

執行package命令

等待編譯成功,會在工程目錄的target文件夾下生成一個jar

運行jar

命令行cdtarget目錄

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