本篇文章将引导你创建一个简单的Spring Boot web程序示例,涉及到的组件有:嵌入的Tomcat + Thymeleaf 模板引擎,可执行的 JAR 文件包。
开发工具:
1.Spring Boot 1.5.3.RELEASE
2.Spring 4.3.8.RELEASE
3.Thymeleaf 2.1.5.RELEASE
4.Tomcat Embed 8.5.14
5.Maven 3
6.Java 8
1.创建项目
eclipse -- File -- New -- Maven Project
2.项目的目录结构如下所示:
手动创建如下的文件夹及文件
3.项目依赖 pom.xml
spring-boot-starter-thymeleaf 这个依赖包将为你提供一切需要的东西来开发一个 Spring + Thymeleaf 程序
pom.xml 文件
4.0.0
org.thinkingingis
spring-boot-web-thymeleaf
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.3.RELEASE
1.8
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-devtools
true
org.webjars
bootstrap
3.3.7
maven-compiler-plugin
3.3
true
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java
显示项目的依赖:在终端输入mvn dependency: tree
$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-boot-web-thymeleaf 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ spring-boot-web-thymeleaf ---
[INFO] org.thinkingingis:spring-boot-web-thymeleaf:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:1.5.3.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.5.3.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.3.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] | | | +- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile
[INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.25:compile
[INFO] | | +- org.springframework:spring-core:jar:4.3.8.RELEASE:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] | +- org.springframework.boot:spring-boot-starter-web:jar:1.5.3.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.3.RELEASE:compile
[INFO] | | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.14:compile
[INFO] | | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.14:compile
[INFO] | | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.14:compile
[INFO] | | +- org.hibernate:hibernate-validator:jar:5.3.5.Final:compile
[INFO] | | | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | | | +- org.jboss.logging:jboss-logging:jar:3.3.1.Final:compile
[INFO] | | | \- com.fasterxml:classmate:jar:1.3.3:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.8:compile
[INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.8.8:compile
[INFO] | | +- org.springframework:spring-web:jar:4.3.8.RELEASE:compile
[INFO] | | | +- org.springframework:spring-aop:jar:4.3.8.RELEASE:compile
[INFO] | | | \- org.springframework:spring-beans:jar:4.3.8.RELEASE:compile
[INFO] | | \- org.springframework:spring-webmvc:jar:4.3.8.RELEASE:compile
[INFO] | | \- org.springframework:spring-expression:jar:4.3.8.RELEASE:compile
[INFO] | +- org.thymeleaf:thymeleaf-spring4:jar:2.1.5.RELEASE:compile
[INFO] | | +- org.thymeleaf:thymeleaf:jar:2.1.5.RELEASE:compile
[INFO] | | | +- ognl:ognl:jar:3.0.8:compile
[INFO] | | | +- org.javassist:javassist:jar:3.21.0-GA:compile
[INFO] | | | \- org.unbescape:unbescape:jar:1.1.0.RELEASE:compile
[INFO] | | \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] | \- nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar:1.4.0:compile
[INFO] | \- org.codehaus.groovy:groovy:jar:2.4.10:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:1.5.3.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:1.5.3.RELEASE:compile
[INFO] | | \- org.springframework:spring-context:jar:4.3.8.RELEASE:compile
[INFO] | \- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.3.RELEASE:compile
[INFO] \- org.webjars:bootstrap:jar:3.3.7:compile
[INFO] \- org.webjars:jquery:jar:1.11.1:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.139 s
[INFO] Finished at: 2017-06-10T14:11:29+08:00
[INFO] Final Memory: 21M/226M
[INFO] ------------------------------------------------------------------------
出现 BUILD SUCCESS 表示成功。
4.Spring Boot
4.1添加@SpringBootApplication注解,运行这个类完成spring boot web程序的启动。
package org.thinkingingis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[] args){
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
4.2 添加controller类 WelcomeController.java
package org.thinkingingis;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WelcomeController {
@Value("${welcome.message:test}")
private String message = "Hello ThinkingInGIS";
@RequestMapping("/")
public String welcome(Map model){
model.put("message", this.message);
return "welcome";
}
}
5. 添加thymeleaf 资源
5.1将thymeleaf模板文件存放在 src/main/resources/templates/ 下
welcome.html
Spring Boot + Thymeleaf Hello World 示例
Spring Boot Web Thymeleaf 示例
5.2 properties文件,放在src/main/resources/ 文件夹下
application.properties
welcome.message: Hello ThinkingInGIS ~ | \u5FAE\u4FE1\u516C\u4F17\u53F7\uFF1A ThinkingInGIS
6.1进入项目目录下:/Users/gisboy/Documents/workspace/spring-boot-web-thymeleaf
6.2输入 mvn spring-boot:run
$ mvn spring-boot:run
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)
出现 BUILD SUCCESS 表示成功
至此,一个简单的spring boot + thymeleaf 程序 就搭建好了。
(如遇到问题,请留言给作者,以便共同探讨gis知识。[email protected])
在下开通了一个微信公众号: ThinkingInGIS
欢迎大家关注:)