springboot thymeleaf ----服務端渲染html


一、 引用命名空間 <html xmlns:th="http://www.thymeleaf.org"> 不這么寫 html標簽沒閉合會報錯

 

二、實際內容在../static//font-awesome/css/font-awesome.css

引入css
<link th:href="@{/font-awesome/css/font-awesome.css}" rel="stylesheet" />

引入js
<script type="text/javascript" th:src="@{/js/jquery-1.11.2.min.js}"></script>

 

三、輸出內容

<p th:text="#{home.welcome}">Welcome to our grocery store!</p>
th:utext 用來顯示“unescaped ” 的html內容
th:text="${today}" ${today} 用來引用 today 變量

 

四、 

訪問對象
${param.x} 返回名為x 的 request參數。(可能有多個值)

${session.x} 返回名為x的Session參數。

${application.x} 返回名為 servlet context 的參數。

 

五、

#{home.welcome(${session.user.name})} -- 格式化數據

#ctx: the context object.
#vars: the context variables.
#locale: the context locale.
#request: (only in Web Contexts) the HttpServletRequest object.
#response: (only in Web Contexts) the HttpServletResponse object.
#session: (only in Web Contexts) the HttpSession object.
#servletContext: (only in Web Contexts) the ServletContext object.

日期:
${#calendars.format(today,'dd MMMM yyyy')}

 

六、

星號語法

<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

 

七、

輸出URL

<a href="product/list.html" th:href="@{/product/list}">Product List</a>

<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

 

八、

使用代碼段

<div th:insert="~{commons :: main}">...</div>
<div th:if="${user.isAdmin()} == false"> --輸出布爾表達式

<tr th:class="${user.id}? 'even' : 'odd'">
...
</tr>

 

九、

預處理

<p th:text="${__#{article.text('textVar')}__}">Some text here...</p>


設置任何Attribute 的方法

<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/> --設置單個

<img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" /> --一次設置多個

 

十、

循環
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>


免責聲明!

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



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