SpringBoot的環境搭建

首先須要引入jar包html

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.3.5.RELEASE</version>
  <type>pom</type>
</dependency>

<!--<dependency>-->
  <!--<groupId>org.springframework.boot</groupId>-->
  <!--<artifactId>spring-boot-starter-web</artifactId>-->
  <!--<version>1.5.4.RELEASE</version>-->
<!--</dependency>-->

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
  <version>1.0.2.RELEASE</version>
</dependency>

第二個jar包註釋掉了,由於第三個jar包裏面已經依賴了它。第三個jar包是視圖解析器web

接下來,就能夠寫啓動類了spring

package com;

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

/**
 * Created by zhangsan 2017/8/24 0024.
 */
//啓動類的註解
@SpringBootApplication
public class ApplicationStart {
    //main方法啓動
    public static void main(String[] args) {
        SpringApplication.run(ApplicationStart.class);
    }
}

注意:啓動類的包必定在其餘contrller、service、dao等的上級。由於springboot只掃描啓動類子包下面的類。springboot

而後咱們寫一個頁面,目錄結構以下app

爲何要放在templates包下呢,由於thymeleaf視圖解析器默認是在這個目錄下找html文件的。固然這個是能夠配置的,配置就是在application.properties裏了spring-boot

而後,咱們寫一個controller試試吧htm

package com.mmm.controller;

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

/**
 * Created by zhangsan on 2017/8/24 0024.
 * 此處註解用Controller,而不是RestController
 * RestController = Controller + ResponseBody
 * 因此,若是使用RestController,則至關於給每一個方法都
 * 加了ResponseBody註解,而這個註解的意思是,方法的返回值是什麼
 * 就返回什麼內容。因此視圖解析器就不會產生做用了
 */
@Controller
public class ViewController {
    @RequestMapping("/index")
    public String redirect(){
        return "index";
    }
}

訪問結果io

OK,成功。class

相關文章
相關標籤/搜索