因爲在開發My Blog項目時使用了大量的技術整合,針對於部分框架的使用和整合的流程沒有作詳細的介紹和記錄,致使有些朋友用起來有些吃力,所以打算在接下來的時間裏作一些基礎整合的介紹,固然,可能也不會特別的基礎,可是源碼會開放給你們,方便你們學習,這次的源碼地址爲springboot-thymeleaf,多謝你們支持。html
Thymeleaf是一個跟Velocity、FreeMarker相似的模板引擎,它能夠徹底替代JSP,相較與其餘的模板引擎,它有以下三個極吸引人的特色:java
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>com.my.blog</artifactId> <name>springboot-thymeleaf</name> <description>springboot-thymeleaf</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
# thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML5 spring.thymeleaf.cache=false
在resources文件夾下新增templates目錄,用於存放模板文件,新增hello.html。git
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>springboot-thymeleaf demo</title> </head> <body> <p th:text="'hello, ' + ${name} + '!'" /> </body> </html>
HelloController :程序員
/** * author:13 * date:2017-09-14 */ @Controller public class HelloController { @RequestMapping("/hello") public String hello(HttpServletRequest request, @RequestParam(value = "name", required = false, defaultValue = "springboot-thymeleaf") String name) { request.setAttribute("name", name); return "hello"; } }
WebApplication :github
/** * author:13 * date:2017-09-14 */ @SpringBootApplication public class WebApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(WebApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(WebApplication.class, args); } }
項目啓動後,在瀏覽器端輸入如下urlhttp://localhost:8080/hello
:
spring
首發於個人我的博客。apache
若是有問題或者有一些好的創意,歡迎給我留言,也感謝向我指出項目中存在問題的朋友。瀏覽器
代碼和此次的問題都是My Blog項目中的,若是你想繼續瞭解該項目能夠查看整個系列文章Java開源博客My-Blog(SpringBoot+Docker)系列文章,也能夠到個人GitHub倉庫或者開源中國代碼倉庫中查看源碼及詳細的部署過程和使用文檔。springboot