國(guó)際貿(mào)易網(wǎng)站排名最新網(wǎng)絡(luò)營(yíng)銷方式有哪些
Thymeleaf 支持 HTML 原型,可以讓前端工程師在瀏覽器中直接打開查看樣式,也可以讓后端工程師結(jié)合真實(shí)數(shù)據(jù)查看顯示效果
Thymeleaf 除了展示基本的 HTML ,進(jìn)行頁(yè)面渲染之外,也可以作為一個(gè) HTML 片段進(jìn)行渲染,例如我們?cè)谧鲟]件發(fā)送時(shí),可以使用 Thymeleaf 作為郵件發(fā)送模板
SpringBoot 提供了 Thymeleaf 自動(dòng)化配置解決方案:
這些默認(rèn)的配置我們幾乎不需要做任何更改就可以直接使用了。如果開發(fā)者有特殊需求,則可以在 application.properties 中配置以 spring.thymeleaf 開頭的屬性即可
①、配置類屬性:org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
- 首先通過 @ConfigurationProperties 注解,將 application.properties 前綴為 spring.thymeleaf 的配置和這個(gè)類中的屬性綁定
- 前三個(gè) static 變量定義了默認(rèn)的編碼格式、視圖解析器的前綴、后綴等
- 從前三行配置中,可以看出來,Thymeleaf 模板的默認(rèn)位置在 resources/templates 目錄下,默認(rèn)的后綴是 html
- 這些配置,如果開發(fā)者不自己提供,則使用 默認(rèn)的,如果自己提供,則在 application.properties 中以 spring.thymeleaf 開始相關(guān)的配置
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {private staticfinal Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;public staticfinal String DEFAULT_PREFIX = "classpath:/templates/";public staticfinal String DEFAULT_SUFFIX = ".html";private boolean checkTemplate = true;private boolean checkTemplateLocation = true;private String prefix = DEFAULT_PREFIX;private String suffix = DEFAULT_SUFFIX;private String mode = "HTML";private Charset encoding = DEFAULT_ENCODING;private boolean cache = true;//...
}
②、配置類:org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
- 首先導(dǎo)入 ThymeleafProperties
- 然后 @ConditionalOnClass 注解表示當(dāng)當(dāng)前系統(tǒng)中存在 TemplateMode 和 SpringTemplateEngine 類時(shí),當(dāng)前的自動(dòng)化配置類才會(huì)生效(即只要項(xiàng)目中引入了 Thymeleaf 相關(guān)的依賴,這個(gè)配置就會(huì)生效)
@Configuration
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
publicclass ThymeleafAutoConfiguration {
}
由于 Thymeleaf 模板后綴為 .html,可以直接被瀏覽器打開
一、創(chuàng)建項(xiàng)目
創(chuàng)建完成后,pom.xml 依賴如下:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
二、創(chuàng)建Controller
@Controller
public class IndexController{@GetMapping("/index")public String index(Model model){List<User> users = new ArrayList<>();for(int i = 0; i < 10; i++){User u = new User();u.setId((long) i);u.setName("javaboy:" + i);u.setAddress("深圳:" + i);users.add(u);}//在 IndexController 中返回邏輯視圖名+數(shù)據(jù),邏輯視圖名為 indexmodel.addAttribute("users", users);return "index";}
}
需要在 resources/templates 目錄下提供一個(gè)名為 index.html 的 Thymeleaf 模板文件
<!DOCTYPE html><!--thymeleaf 名稱空間-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<table border="1"><tr><td>編號(hào)</td><td>用戶名</td><td>地址</td></tr><!--對(duì)model.addAttribute("users", users);進(jìn)行遍歷--><tr th:each="user : ${users}"><td th:text="${user.id}"></td><td th:text="${user.name}"></td><td th:text="${user.address}"></td></tr>
</table>
</body>
</html>
配置完成后,就可以啟動(dòng)項(xiàng)目了,訪問 /index 接口,就能看到集合中的數(shù)據(jù)了
三、Thymeleaf 支持在 js 中直接獲取 Model 中的變量
@Controller
public class IndexController {@GetMapping("/index")public String index(Model model) {model.addAttribute("username", "李四");return"index";}
}
<script th:inline="javascript">var username = [[${username}]];console.log(username)
</script>
四、手動(dòng)渲染
一般在郵件發(fā)送時(shí)候有用,例如我在 resources/templates 目錄下新建一個(gè)郵件模板
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<p>hello 歡迎 <span th:text="${username}"></span>加入 XXX 集團(tuán),您的入職信息如下:</p>
<table border="1"><tr><td>職位</td><td th:text="${position}"></td></tr><tr><td>薪水</td><td th:text="${salary}"></td></tr>
</table>
<img src="http://www.javaboy.org/images/sb/javaboy.jpg" alt="">
</body>
</html>
這一個(gè) HTML 模板中,有幾個(gè)變量,要將這個(gè) HTML 模板渲染成一個(gè) String 字符串,再把這個(gè)字符串通過郵件發(fā)送出去
- 首先注入一個(gè) TemplateEngine 對(duì)象,這個(gè)對(duì)象就是在 Thymeleaf 的自動(dòng)化配置類中配置的(即當(dāng)我們引入 Thymeleaf 的依賴之后,這個(gè)實(shí)例就有了)
- 然后構(gòu)造一個(gè) Context 對(duì)象用來存放變量
- 調(diào)用 process 方法進(jìn)行渲染,該方法的返回值就是渲染后的 HTML 字符串,然后我們將這個(gè)字符串發(fā)送出去
@Autowired
TemplateEngine templateEngine;@Test
public void test1() throws MessagingException{Context context = new Context();context.setVariable("username", "javaboy");context.setVariable("position", "Java工程師");context.setVariable("salary", 99999);String mail = templateEngine.process("mail", context);//省略郵件發(fā)送
}