日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot + Thymeleaf 创建web项目

發布時間:2025/3/13 javascript 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot + Thymeleaf 创建web项目 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本篇文章將引導你創建一個簡單的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 文件

<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 這是告訴可執行的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 系統 使用如下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] ------------------------------------------------------------------------

出現 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

<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Spring Boot + Thymeleaf Hello World 示例</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" /></head> <body><nav class="navbar navbar-inverse"><div class="container"><div class="navbar-header"><a class="navbar-brand" href="#">Spring Boot</a></div><div id="navbar" class="collapse navbar-collapse"><ul class="nav navbar-nav"><li class="active"><a href="#">Home</a></li></ul></div></div></nav><div class="container"><div class="starter-template"><h1 style="color: #4EE2EC">Spring Boot Web Thymeleaf 示例</h1><h2 style="color: #ED594E"><span th:text="'Message: ' + ${message}"></span></h2></div></div><script type="text/javascript" src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script></body> </html>5.2 properties文件,放在src/main/resources/ 文件夾下

application.properties

welcome.message: Hello ThinkingInGIS ~ | \u5FAE\u4FE1\u516C\u4F17\u53F7\uFF1A ThinkingInGIS
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) 出現 BUILD SUCCESS? 表示成功

6.3訪問 http://localhost:8080

至此,一個簡單的spring boot + thymeleaf 程序 就搭建好了。

(如遇到問題,請留言給作者,以便共同探討gis知識。thinkingingis@qq.com)

在下開通了一個微信公眾號: ThinkingInGIS?


歡迎大家關注:)



總結

以上是生活随笔為你收集整理的Spring Boot + Thymeleaf 创建web项目的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。