關於springboot想必不少人都在使用,因爲公司項目一直使用的是SpringMVC,因此本身抽空體驗了一下springboot的簡單使用。javascript
springbooot的環境搭建能夠說很靈活,能夠新建maven普通項目來手動搭建,固然也可使用Spring的STS來搭建,因爲IDE使用eclipse,因此就直接使用STS插件。css
1. 在eclipse的Marketplace中搜索STS,後直接下載安裝便可。html
2. 安裝好後,在新建項目中就能夠看到springboot的相關內容,選擇第二個新建springboot前端
3. 第三步就是選擇對應的依賴,固然你能夠將須要的依賴所有添加,也能夠先添加當前須要的,隨後用到什麼在添加。java
Springboot的啓動很簡單,以下:jquery
1 package com.yongcheng.liuyang; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class DemoApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(DemoApplication.class, args); 11 } 12 }
其中最主要的是@SpringBootApplication的註解,固然從啓動過程也能夠發現,springboot其實質仍是運行在web容器中,web
接下來就寫一個springmvc中常常用到的Controller很簡單的測試下,固然前端頁面咱們選擇了官方推薦的thymeleaf,spring
因此首先要在pom.xml中導入這個依賴包,以下:json
<!-- thymeleaf模板引擎包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>bootstrap
controller的寫法和springmvc同樣,熟悉springmvc的基本不用多說,直接上代碼:
1 package com.yongcheng.liuyang.controller; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.ui.Model; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 9 import com.yongcheng.liuyang.entity.AuthorSetting; 10 11 @Controller 12 @RequestMapping(value="/") 13 public class UserLoginController { 14 15 private static final Logger logger = LoggerFactory.getLogger(UserLoginController.class); 16 17 18 @RequestMapping("/test") 19 public String testthymeleaf(Model model) 20 { 21 model.addAttribute("singlePerson", new AuthorSetting("zhangsan",18)); 22 return "viewTest"; 23 } 24 }
因爲咱們須要返回頁面因此使用的是@Controller註解,固然若是你要返回json數據,那麼你的註解固然是@RestController
接下就是前臺的頁面
1 <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" 3 xmlns:th="http://www.thymeleaf.org"> 4 <head> 5 <meta content="text/html" charset="UTF-8"> 6 <link th:src="@{/css/bootstrap.min.css}" rel="stylesheet"> 7 <link th:src="@{/css/bootstrap-theme.min.css}" rel="stylesheet"> 8 9 <script th:src="@{/js/jquery-3.2.1.min.js}" type="text/javascript"></script> 10 <script th:src="@{/js/bootstrap.min.js}" type="text/javascript"></script> 11 12 <title>thymeleaf簡單實用</title> 13 </head> 14 <body> 15 <div class="panel panel-primary"> 16 <div class="panel-heading"> 17 <h3 class="panel-title">訪問model</h3> 18 </div> 19 <div class="panel-body"> 20 <span th:text="${singlePerson.name}"></span> 21 </div> 22 </div> 23 </body> 24 </html>
注意html文件中引入xmlns:th="http://www.thymeleaf.org",其次就是這裏的動態加載使用th:dom選擇,好比項目中的th:text,
URL使用@{},好比項目中css和js的引入。
這樣子整個問題就算解決了嗎?沒有,瞭解springmvc的都知道,springmvc在解析JSP文件時須要配置對應的文件先後綴,好比以下的配置:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver" > <!-- 自動添加到路徑中的前綴 --> <property name="prefix" value="/jsp/" /> <!-- 自動添加到路徑中的後綴 --> <property name="suffix" value=".jsp" /> </bean>
在springboot中同樣也要配置這樣的,其中springboot的全部配置信息都在application.properties或者application.yml文件中,
那麼如何配置對應的thymeleaf的視圖解析配置參數呢?
因爲小編選擇的是yml,配置參數以下:
spring: thymeleaf: prefix: "classpath:/templates/" suffix: ".html"
固然若是你的項目結構絕對嚴謹的話,好比使用STS建立的項目,那麼其實上面的是能夠不配置的,具體緣由可參見ThymeleafProperties類。
重要提示:
爲了讓項目正常加載bean,main函數所在類的必定要是其它類的父包,不然沒法掃描到配置的bean。
好了,以上就是springboot+thymeleaf的簡單使用。