thymeleaf是前臺頁面展現,原來一直是jsp,jsp中包含不少服務器端的邏輯,逐漸淘汰。一樣功能的還有freemarker。孰好孰壞不予評價,只作簡單實現。html
一、基本思路java
(1)pom.xml中配置依賴。mysql
(2)寫頁面。git
(3)Controller的改動。github
二、實現web
(1)須要在pom.xml中添加代碼以下:redis
<!-- thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
pom.xml代碼以下spring
<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> <groupId>com.sun</groupId> <artifactId>spring-boot-test</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 熱部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!-- redis cache related.....start --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
(2)頁面寫個獲得全體User的展示頁面吧。sql
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <h1 th:inline="text">Hello World!</h1> <p id="userList" th:text="${userList}"></p> <th:block th:each="user,index:${userList}"> <span th:text="${user.id}" ></span> <span th:text="${user.username}" ></span> <span th:text="${user.password}" ></span> <br/> </th:block> </body> </html>
(3)修改Controller。修改代碼以下:apache
@RequestMapping("/all") public String findAll(Map<String,Object> map){ List<User> userList = userMapper.selectAll(); map.put("hello","PPBOY"); map.put("userList",userList); return "/test"; }
Controller源碼以下,記得把@RestController換成@Controller:
package com.sun.controller; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.sun.beans.RedisCacheUtil; import com.sun.dao.UserMapper; import com.sun.model.User; @Controller @RequestMapping("/test") public class TestController { @Resource private UserMapper userMapper; @RequestMapping("/hello") public String hello(){ return "Hello world!"; } @RequestMapping("/all") public String findAll(Map<String,Object> map){ List<User> userList = userMapper.selectAll(); map.put("hello","PPBOY"); map.put("userList",userList); return "/test"; } @Autowired private RedisCacheUtil<User> redisCache; @RequestMapping("/testGetCache") public String testGetCache() { System.out.println("------------user"); Map<Integer,User> userMap = redisCache.getCacheIntegerMap("userMap"); for(int key : userMap.keySet()) { System.out.println("key = " + key + ",value=" + userMap.get(key)); } return "/test"; } }
三、總結
(1)模板引擎最終仍是要和Controller作聯繫的,因此二者的關聯是重點。
須要在controller的對應方法中return一個字符串是文件路徑,好比return "/test"。
(2)頁面訪問地址須要的@RequestMapping("/") 這個寫到Controller的方法中,訪問這個方法,就是http://localhost:8080/就能夠了
(3)爲何個人項目代碼在myeclipse中會給個 Initializing Spring Project '***'". java.lang.IllegalArgumentException這樣的提示,可是啓動項目沒問題啊??媽的,我懷疑我myeclipse有問題。
(4)模板引擎要放到resources下的templates文件夾下。
(5)最後還要記得在application.properties中添加代碼關閉模板引擎的緩存。
######################################################## ###THYMELEAF (ThymeleafAutoConfiguration) ######################################################## #spring.thymeleaf.prefix=classpath:/templates/ #spring.thymeleaf.suffix=.html #spring.thymeleaf.mode=HTML5 #spring.thymeleaf.encoding=UTF-8 # ;charset=<encoding> is added #spring.thymeleaf.content-type=text/html # set to false for hot refresh spring.thymeleaf.cache=false
源碼已上傳https://github.com/sunfengjiajia/spring-boot-test