您好,登錄后才能下訂單哦!
Spring Boot中,
靜態資源(css、js、圖片等)默認放在resources/static下面。如果要修改默認存放目錄,可以通過設置屬性 spring.mvc.static-path-pattern來實現。
模板文件默認放在 templates目錄下
Spring boot支持使用模板來開發web應用,支持的模板類型包括
FreeMarker
Groovy
Thymeleaf
Mustache
Spring boot不建議使用jsp開發web。
本文使用Thymeleaf作為模板引擎來開發web項目。
一、在application.properties中設置Thymeleaf相關屬性
# Check that the template exists before rendering it. spring.thymeleaf.check-template=true # Check that the templates location exists. spring.thymeleaf.check-template-location=true # Content-Type value written to HTTP responses. spring.thymeleaf.content-type=text/html # Enable Thymeleaf view resolution for Web frameworks. spring.thymeleaf.enable=true # Template files encoding. spring.thymeleaf.encoding=UTF-8 # Comma-separated list of view names that should be excluded from resolution. spring.thymeleaf.exclude-view-names=index # Prefix that gets prepended to view names when building a URL. spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html # Template mode to be applied to templates. See also StandardTemplateModeHandlers. spring.thymeleaf.mode=HTML5 # Disable template caching. spring.thymeleaf.cache=false
二、在pom.xml添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
三、在resources目錄下創建templates文件夾,并添加模板html
test.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 姓名:<span th:text="${user.username}"></span></br> 郵箱:<span th:text="${user.email}"></span></br> 昵稱:<span th:text="${user.nickname}"></span> </body> </html>
四、增加Controller
@Controller public class TemplatesController { @Autowired private UserService userService; @RequestMapping("/index") String test(ModelMap map) { map.addAttribute("key", "thymeleaf"); return "index"; } @RequestMapping("/test") public String testThymeleaf(ModelMap map) { map.addAttribute("user", userService.selectByPrimaryKey(1)); return "test"; } }
五、運行結果
到這里,Spring Boot + Mybatis +Spring MVC環境就搭建完成了
完整環境下載地址: https://github.com/CatherineHu/Spring-Boot-Mybatis-MVC
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。