springboot+thymeleaf簡單使用


 關於springboot想必很多人都在使用,由於公司項目一直使用的是SpringMVC,所以自己抽空體驗了一下springboot的簡單使用。

  • 環境搭建

      springbooot的環境搭建可以說很靈活,可以新建maven普通項目來手動搭建,當然也可以使用Spring的STS來搭建,由於IDE使用eclipse,所以就直接使用STS插件。

     1. 在eclipse的Marketplace中搜索STS,后直接下載安裝即可。

          

    2.  安裝好后,在新建項目中就可以看到springboot的相關內容,選擇第二個新建springboot

         

   3. 第三步就是選擇對應的依賴,當然你可以將需要的依賴全部添加,也可以先添加當前需要的,隨后用到什么在添加。

 


 

  •  代碼結構

   Springboot的啟動很簡單,如下:

 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容器中,

    

 

        接下來就寫一個springmvc中經常用到的Controller很簡單的測試下,當然前端頁面我們選擇了官方推薦的thymeleaf,

   所以首先要在pom.xml中導入這個依賴包,如下:

            <!-- thymeleaf模板引擎包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>

    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>
View Code

注意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的簡單使用。

 

 
        

 

 

 

 

 

 

 

 

     

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM