| 知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
#靜態資源訪問路徑 spring.mvc.static-path-pattern=/** #靜態資源映射路徑 spring.resources.static-locations=classpath:/
package cn.netkiller.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");
}
}
cssClass 使用該屬性指定表單元素CSS樣式名,相當於HTML元素的class屬性
<form:input path="userName" cssClass="inputStyle"/>
cssStyle 直接通過該屬性指定樣式,相當於HTML元素的style屬性
<form:input path="userName" cssStyle="width:100px"/>
http://thymeleaf.org/
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
<!-- **************************************************************** --> <!-- THYMELEAF-SPECIFIC ARTIFACTS --> <!-- TemplateResolver <- TemplateEngine <- ViewResolver --> <!-- **************************************************************** --> <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"> <property name="prefix" value="/WEB-INF/templates/" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML5" /> </bean> <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver" /> </bean> <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine" /> </bean>
package cn.netkiller.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class HelloController {
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String getMovie(@PathVariable String name, ModelMap model) {
model.addAttribute("name", name);
return "hello";
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Spring MVC + Thymeleaf Example</title>
</head>
<body>
Hello, <span th:text="${name}" />!
</body>
</html>
@RequestMapping("/list")
public ModelAndView list() {
Iterable<User> users = userRepository.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("users", users);
mv.setViewName("table");
return mv;
}
模板檔案
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>用戶登記</title>
</head>
<body>
<h1>Welcome to Thymeleaf</h1>
<table border="1" width="100%">
<tr>
<td>ID</td>
<td>姓名</td>
<td>聯繫方式</td>
<td>詳細地址</td>
<td>圖片</td>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.tel}"></td>
<td th:text="${user.address}"></td>
<td th:text="${user.picture}"></td>
</tr>
</table>
</body>
</html>
<span th:text="${number+1}"></span> /
<span th:text="${totalPages}"></span>
<a href="#"
th:href="@{/api/user/browse?sort=id,desc&size=10(page=${number-1})}">上一頁</a>
<a href="#"
th:href="@{/api/user/browse?sort=id,desc&size=10(page=${number+1})}">下一頁</a>
拼接 URL 的方法
<img src="#" th:src="${'https://img.netkiller.cn/' + pic}" height="128" th:target="_blank" />
pictures 是一個以逗號分割得字元串。我們需要拆分並逐條顯示。
<div th:unless="${picture == null}">
<a th:each="pic : ${#strings.arraySplit(pictures, ',')}" href="#" th:href="${pic}"> <img src="#" th:src="${pic}" height="64" /></a>
</div>
<span th:text="${#dates.format(createDate, 'yyyy-MM-dd HH:mm')}"></span
// java.util.Date 處理
${#dates.day(date)}
${#dates.month(date)}
${#dates.monthName(date)}
${#dates.monthNameShort(date)}
${#dates.year(date)}
${#dates.dayOfWeek(date)}
${#dates.dayOfWeekName(date)}
${#dates.dayOfWeekNameShort(date)}
${#dates.hour(date)}
${#dates.minute(date)}
${#dates.second(date)}
${#dates.millisecond(date)}
// java.time 時間處理
${#temporals.day(date)}
${#temporals.month(date)}
${#temporals.monthName(date)}
${#temporals.monthNameShort(date)}
${#temporals.year(date)}
${#temporals.dayOfWeek(date)}
${#temporals.dayOfWeekName(date)}
${#temporals.dayOfWeekNameShort(date)}
${#temporals.hour(date)}
${#temporals.minute(date)}
${#temporals.second(date)}
${#temporals.millisecond(date)}
// 處理天實例
<p th:text="${#dates.day(standardDate)}"></p>
<p th:text="${#temporals.day(localDateTime)}"></p>
<p th:text="${#temporals.day(localDate)}"></p>
// 處理周實例
<p th:text="${#dates.dayOfWeekName(standardDate)}"></p>
<p th:text="${#temporals.dayOfWeekName(localDateTime)}"></p>
<p th:text="${#temporals.dayOfWeekName(localDate)}"></p>
// 處理秒實例
<p th:text="${#dates.second(standardDate)}"></p>
<p th:text="${#temporals.second(localDateTime)}"></p>