javascript
Spring Boot + Thymeleaf 创建web项目
本篇文章將引導你創(chuàng)建一個簡單的Spring Boot web程序示例,涉及到的組件有:嵌入的Tomcat + Thymeleaf 模板引擎,可執(zhí)行的 JAR 文件包。
開發(fā)工具:
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.創(chuàng)建項目
eclipse -- File -- New -- Maven Project
2.項目的目錄結(jié)構(gòu)如下所示:
手動創(chuàng)建如下的文件夾及文件
3.項目依賴 pom.xml
spring-boot-starter-thymeleaf 這個依賴包將為你提供一切需要的東西來開發(fā)一個? Spring + Thymeleaf 程序
pom.xml 文件
<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>org.thinkingingis</groupId><artifactId>spring-boot-web-thymeleaf</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version></parent><properties><java.version>1.8</java.version></properties><dependencies><!-- 核心依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><!-- bootstrap 依賴--><dependency><groupId>org.webjars</groupId><artifactId>bootstrap</artifactId><version>3.3.7</version></dependency></dependencies><build><plugins><!-- macOS 使用如下plugin 這是告訴可執(zhí)行的jar文件位置,具體情況視你jdk的安裝位置而定--><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.3</version><configuration><fork>true</fork><executable>/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java</executable></configuration></plugin><!--windows 系統(tǒng) 使用如下plugin --><!-- <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin>--></plugins></build></project>顯示項目的依賴:在終端輸入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] ------------------------------------------------------------------------出現(xiàn) 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<String, Object> model){model.put("message", this.message);return "welcome";}}5. 添加thymeleaf 資源5.1將thymeleaf模板文件存放在 src/main/resources/templates/ 下
welcome.html
application.properties
6. 啟動
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) 出現(xiàn) BUILD SUCCESS? 表示成功6.3訪問 http://localhost:8080
至此,一個簡單的spring boot + thymeleaf 程序 就搭建好了。
(如遇到問題,請留言給作者,以便共同探討gis知識。thinkingingis@qq.com)
在下開通了一個微信公眾號: ThinkingInGIS?
歡迎大家關注:)
總結(jié)
以上是生活随笔為你收集整理的Spring Boot + Thymeleaf 创建web项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 8 forEach 示例
- 下一篇: Java 获取当前时间