springboot web入門之Thymeleaf

來源:互聯網
上載者:User

本篇內容來自 大神文章:http://tengj.top/2017/03/13/springboot4/
本來不打算寫Thymeleaf的,之前一直用的也是jsp,不過springboot官方推薦使用Thymeleaf,在這裡照著大神的文章再寫一遍當作入門吧,水平有限,要學習還是找大神們的吧。

Thymeleaf是一款用於渲染XML/XHTML/HTML5內容的模板引擎。類似JSP,Velocity,FreeMaker等,它也可以輕易的與Spring MVC等Web架構進行整合作為Web應用的模板引擎。與其它模板引擎相比,Thymeleaf最大的特點是能夠直接在瀏覽器中開啟並正確顯示模板頁面,而不需要啟動整個Web應用。

關於Thymeleaf介紹我也說不明白請自行百度,我只知道Thymeleaf可以html實現動態資料載入。

Thymeleaf入門項目步驟:
1.建立springboot項目,座標只選Thymeleaf(已經依賴web)

        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>

2.建立controller

@Controller@RequestMapping("/learn")public class LearnResourceController {    @RequestMapping(method= RequestMethod.GET)    //public String test(ModelMap map){    //也可以    public ModelAndView test(){        List<LearnResouce> learnList =new ArrayList<LearnResouce>();        LearnResouce bean =new LearnResouce("官方參考文檔","Spring Boot Reference Guide","http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#getting-started-first-application");        learnList.add(bean);        bean =new LearnResouce("官方SpriongBoot例子","官方SpriongBoot例子","https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples");        learnList.add(bean);        bean =new LearnResouce("嘟嘟MD獨立部落格","Spring Boot乾貨系列 ","http://tengj.top/");        learnList.add(bean);        bean =new LearnResouce("後端編程嘟","Spring Boot教程和視頻 ","http://www.toutiao.com/m1559096720023553/");        learnList.add(bean);        bean =new LearnResouce("程式猿DD","Spring Boot系列","http://www.roncoo.com/article/detail/125488");        learnList.add(bean);        ModelAndView modelAndView = new ModelAndView("/index");        modelAndView.addObject("learnList", learnList);        //map.addAttribute("learnList", learnList);        return modelAndView;        //return "index";    }}

其中 new ModelAndView(“/index”) 的/index指resource/templates目錄下的index.html頁面返回請求的時候就會根據modelAndView找到指定頁面。

3.建立index.html
引入依賴後就在預設的模板路徑src/main/resources/templates下編寫模板檔案(沒有目錄自行建立),這裡我們建立一個index.html。

<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head>    <title>learn Resources</title>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><div style="text-align: center;margin:0 auto;width: 1000px; ">    <h1>學習資源大奉送(javaLearn)</h1>    <table width="100%" border="1" cellspacing="1" cellpadding="0">        <tr>            <td>作者</td>            <td>教程名稱</td>            <td>地址</td>        </tr>        <!--/*@thymesVar id="learnList" type=""*/-->        <tr th:each="learn : ${learnList}">            <td th:text="${learn.author}">嘟嘟MD</td>            <td th:text="${learn.title}">SPringBoot乾貨系列</td>            <td><a th:href="${learn.url}" target="_blank">點我</a></td>        </tr>    </table></div></body></html>

url:http://localhost:8080/learn

springboot開發web項目一般情況下不要使用src/main/webapp檔案夾,因為springboot預設打包方式是jar包,只有在war包下才會載入上述路徑資源,如果是jar包的話大多數構建工具都會忽略該路徑。
關於Thmeleaf標籤屬性使用可以看這裡:http://www.cnblogs.com/nuoyiamy/p/5591559.html 或自行百度

附上源碼:https://pan.baidu.com/s/1o7XXUTc

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.