spring-boot快速入門

Spring Boot 相當於傳統的Spring Web應用只不過不是作爲war部署到Web容器中,而是可執行的jar包,內嵌一個Web服務器Jetty,在main函數中把上下文設置好並啓動Jetty服務器去監聽端口。


1、 新建maven項目,配置pom.xml文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
        <!--渲染頁面模板用-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
</dependencies>

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

2、創建Application以及配置URL路由信息

@SpringBootApplication
@RestController
@RequestMapping("/classPath")
public class Application {

    @RequestMapping("")
    public String index0() {
        return "index0!";
    }

    @RequestMapping("/")
    public String index() {
        return "index!";
    }

    @RequestMapping("/hello")
    public String hello() {
        return "Hello World!";
    }

    @RequestMapping(value = "/getUser/{id}" , method = RequestMethod.GET)
    public String getUser(@PathVariable("id") String id) {
        System.out.println("id:"+id);
        //return "您傳的id是"+id;
        return String.format("id %s", id);
    }

    @RequestMapping(value = "/posts/{id}",method = RequestMethod.POST)
    public String post(@PathVariable("id") int id) {
        return String.format("post %d", id);
    }


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

@RequestMapping可以註解@Controller類:訪問地址就應該加上“classPath”:http://localhost:8080/classPath/hello

3、使用@PathVariable傳參

在上述例子中,URL中的變量可以用{variableName}來表示,同時在方法的參數中加上@PathVariable("variableName"),那麼當請求被轉發給該方法處理時,對應的URL中的變量會被自動賦值給被@PathVariable註解的參數(能夠自動根據參數類型賦值,例如上例中的int)。

4、對於HTTP請求除了其URL,還需要注意它的方法(Method)。例如我們在瀏覽器中訪問一個頁面通常是GET方法,而表單的提交一般是POST方法。@Controller中的方法同樣需要對其進行區分。

5、可以定義多個@Controller將不同URL的處理方法分散在不同的類中,例如創建另一個controller

@Controller
@RequestMapping("/test")
public class HelloController {

    @RequestMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello";
    }
}

添加html頁面

接下來需要在默認的模板文件夾src/main/resources/templates/目錄下添加一個模板文件hello.html:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!!!!'" />
<div class="red">123</div>
</body>
</html>

訪問地址;http://localhost:8080/test/hello/123

wKioL1lojz6AYWtfAAChJfA1a1g875.png-wh_50

6、頁面模板渲染

在之前所有的@RequestMapping註解的方法中,返回值字符串都被直接傳送到瀏覽器端並顯示給用戶。但是爲了能夠呈現更加豐富、美觀的頁面,我們需要將HTML代碼返回給瀏覽器,瀏覽器再進行頁面的渲染、顯示。


一種很直觀的方法是在處理請求的方法中,直接返回HTML代碼,但是這樣做的問題在於——一個複雜的頁面HTML代碼往往也非常複雜,並且嵌入在Java代碼中十分不利於維護。更好的做法是將頁面的HTML代碼寫在模板文件中,渲染後再返回給用戶。爲了能夠進行模板渲染,需要將@RestController改成@Controller:

在pom文件中添加依賴

<!--渲染頁面模板用-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

7、加載靜態文件

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link type="text/css" rel="stylesheet" href="/css/hello.css" />
    <script type="text/javascript" src="/js/hello.js"></script>
    
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!!!!'" />
<div class="red">123</div>
</body>
</html>

瀏覽器頁面使用HTML作爲描述語言,那麼必然也脫離不了CSS以及JavaScript。爲了能夠瀏覽器能夠正確加載類似/css/style.css, /js/main.js等資源,默認情況下我們只需要在src/main/resources/static目錄下添加css/style.css和js/main.js文件後,Spring MVC能夠自動將他們發佈,通過訪問/css/style.css, /js/main.js也就可以正確加載這些資源。


wKioL1lojiPRwqEAAABwm9X8ZSk214.png-wh_50


根據此地址做的測試:http://blog.csdn.net/xiaoyu411502/article/details/47864969



獲取yml文件數據

application.yml

user:
  path: 123

java文件獲取的話

@Value("${user.path}")
private String userPath;

這樣的話數據就拿到了

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