1径荔、開(kāi)發(fā)環(huán)境搭建
- JDK版本:Oracle JDK 1.8+
- 構(gòu)建工具:Apache Maven 3.3.0+ (bin.zip)
- 開(kāi)發(fā)工具:IntelliJ IDEA 2017.2.6
2吆你、創(chuàng)建spring-boot工程
- 使用idea創(chuàng)建新project:File->New->Project
- 填寫(xiě)項(xiàng)目基本信息:
- 選擇maven依賴(lài):本示例創(chuàng)建web項(xiàng)目歇竟,后期需要其他依賴(lài)再手動(dòng)添加
-
使用默認(rèn)項(xiàng)目名稱(chēng)丧枪,完成項(xiàng)目創(chuàng)建
完成創(chuàng)建.png
3俄删、項(xiàng)目說(shuō)明
- 目錄結(jié)構(gòu)
目錄結(jié)構(gòu)遵循maven的一般目錄結(jié)構(gòu)疗锐,java中為代碼愉豺;resources中為資源允乐,包括應(yīng)用配置及頁(yè)面等靜態(tài)資源等矮嫉;
- pom說(shuō)明
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springboot.demo</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(1)、parent標(biāo)簽牍疏,指明SpringBoot版本為2.0.6.RELEASE蠢笋。
(2)、starter-web依賴(lài)spring-boot-starter-web鳞陨,它內(nèi)置了tomcat容器昨寞。
(3)、maven打包插件依賴(lài)厦滤,spring-boot-maven-plugin援岩。
- 啟動(dòng)主類(lèi)
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args)
}
}
通過(guò)運(yùn)行主類(lèi)的main方法運(yùn)行整個(gè)項(xiàng)目。
4掏导、配置thymeleaf模板引擎
spring-boot支持如下模板引擎:
- FreeMarker
- Groovy
- Thymeleaf(官方推薦)
- Mustache
本環(huán)境以Thymeleaf為列進(jìn)行web環(huán)境搭建享怀。
- maven依賴(lài)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
可以查看依賴(lài)關(guān)系,發(fā)現(xiàn)spring-boot-starter-thymeleaf下面已經(jīng)包括了spring-boot-starter-web,所以可以把spring-boot-starter-web的依賴(lài)去掉.
- 視圖解析器配置
spring-boot很多配置都有默認(rèn)配置,比如默認(rèn)頁(yè)面映射路徑為classpath:/templates/*.html,同樣靜態(tài)文件路徑為classpath:/static/趟咆,在application.properties中可以配置thymeleaf模板解析器屬性添瓷。
#thymelea模板配置
spring.thymeleaf.prefix=classpath:/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
具體可以配置的參數(shù)可以查看 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties這個(gè)類(lèi),上面的配置實(shí)際上就是注入到該類(lèi)中的屬性值。
- 編寫(xiě)控制器
@Controller
@RequestMapping("/")
public class TestController {
@RequestMapping("")
public String index(){
return "/index.html";
}
}
- 前端頁(yè)面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>歡迎</title>
</head>
<body>
<h4>歡迎使用spring-boot值纱!</h4>
</body>
</html>
- 運(yùn)行效果
spring-boot默認(rèn)web端口為8080鳞贷,效果如下:
5、完整項(xiàng)目
完整目錄結(jié)構(gòu):
相關(guān)閱讀:
spring-boot配置詳解【http://www.reibang.com/p/1d037ab638ef】
spring-boot+druid+mybatis環(huán)境搭建【http://www.reibang.com/p/e6c9e9945e45】
spring-boot+logback+log4j2+MDC【http://www.reibang.com/p/51da61a425ba】