經過ThymeleafProperties來配置Thymeleaf,在application.properies中以spring.thymeleaf開頭進行配置。 javascript
@ConfigurationProperties("spring.thymeleaf") public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8"); private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html"); public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; private boolean checkTemplateLocation = true; private String prefix = "classpath:/templates/"; private String suffix = ".html"; private String mode = "HTML5"; private Charset encoding; private MimeType contentType; private boolean cache; private Integer templateResolverOrder; private String[] viewNames; private String[] excludedViewNames; private boolean enabled; }
用於模板中展現數據的Beancss
/** * @author Kevin * @description * @date 2016/7/7 */ public class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
靜態文件html
默認腳本樣式、圖片等靜態資源放置在src/main/resources/static下java
默認頁面模板放置在src/main/resources/templates下jquery
index.html頁面web
<html xmlns:th="http://www.thymeleaf.org"> <head> <meta content="text/html;charset=UTF-8"/> <meta http-equiv="x-ua-compatible" content="IE=edge"/> <meta name="viewport" content="width=device-width,initial-scale=1"/> <link th:href="@{bootstrap/css/bootstrap.min.css}" rel="stylesheet"/> <link th:href="@{bootstrap/css/bootstrap-theme.min.css}" rel="stylesheet"/> </head> <body> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">訪問model</h3> </div> <div class="panel-body"> <span th:text="${singlePerson.name}"></span> </div> </div> <div th:if="${not #lists.isEmpty(people)}"> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">列表</h3> </div> <div class="panel-body"> <ul class="list-group"> <li class="list-group-item" th:each="person:${people}"> <span th:text="${person.name}"></span> <span th:text="${person.age}"></span> <button class="btn" th:onclick="'getName(\'' + ${person.name} + '\')'">得到名字</button> </li> </ul> </div> </div> </div> <script th:src="@{jquery.min.js}" type="text/javascript"></script> <script th:src="@{bootstrap/js/bootstrap.min.js}" type="text/javascript"></script> <script th:inline="javascript"> var single = [[${singlePerson}]]; console.log(single.name + "/" + single.age); function getName(name){ console.log(name); } </script> </body> </html>
控制器spring
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.List; /** * @author Kevin * @description * @date 2016/7/7 */ @Controller public class MainController { @RequestMapping("/index") public String index(Model model) { Person single = new Person("kevin", 28); List<Person> people = new ArrayList<Person>(); Person p1 = new Person("xx", 11); Person p2 = new Person("yy", 22); Person p3 = new Person("zz", 33); people.add(p1); people.add(p2); people.add(p3); model.addAttribute("singlePerson", single); model.addAttribute("people", people); return "index"; } }
運行bootstrap
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author Kevin * @description * @date 2016/7/7 */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }