springboot入門_模板
阿新 • • 發佈:2018-02-23
content author jar attribute 圖片 -- 添加 ext lis
springboot中已經不推薦使用jsp,而是推薦使用模板,如freemarker,thymeleaf等,本文記錄在sprigboot中使用模板。
創建一個maven的springboot工程,
freemarker,要使用freemarker模板需引入所需要的jar,pom.xml如下:
1 <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"View Code> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>com.allen.springboot.learn</groupId> 4 <artifactId>springboot_freemarker</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 7 <parent> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-parent</artifactId> 10 <version>1.5.10.RELEASE</version> 11 </parent> 12 13 <dependencies> 14 <!-- web 依賴 --> 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-web</artifactId> 18 </dependency> 19 20 <!-- Freemarker 依賴 --> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-freemarker</artifactId> 24 </dependency> 25 </dependencies> 26 27 </project>
在resources目錄下創建application.properties文件,添加freemarker配置信息,代碼如下:
1 ##FREEMARKER 2 spring.freemarker.template-loader-path=classpath:/view/ 3 spring.freemarker.suffix=.ftl 4 #spring.freemarker.prefix= 5 spring.freemarker.allow-request-override=false 6 spring.freemarker.cache=true 7 spring.freemarker.check-template-location=true 8 spring.freemarker.charset=UTF-8 9 spring.freemarker.content-type=text/html 10 spring.freemarker.expose-request-attributes=false 11 spring.freemarker.expose-session-attributes=false 12 spring.freemarker.expose-spring-macro-helpers=falseView Code
這些配置信息中,比較重要的是前兩行,分別指定文件所在路徑和文件名的後綴。
至此freemarker的配置已經完成,接下來創建controller,實體類和模板,使用freemarker模板
1 /** 2 * 3 */ 4 package com.allen.springboot.learn.controller; 5 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import org.springframework.stereotype.Controller; 10 import org.springframework.ui.Model; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 13 import com.allen.springboot.learn.entity.UserInfo; 14 15 /** 16 * @author admin 17 * 18 */ 19 @Controller 20 @RequestMapping("/freemarker") 21 public class FreemarkerController { 22 23 @RequestMapping("/hello") 24 public String hello(Model model){ 25 UserInfo u1 = new UserInfo(); 26 u1.setId(1); 27 u1.setEmail("[email protected]"); 28 u1.setAge(12); 29 u1.setPassword("111111"); 30 u1.setSex("男"); 31 u1.setUsername("allen"); 32 model.addAttribute("u1", u1); 33 34 List<UserInfo> userList = new ArrayList<UserInfo>(); 35 userList.add(u1); 36 UserInfo u2 = new UserInfo(); 37 u2.setId(2); 38 u2.setEmail("[email protected]"); 39 u2.setAge(22); 40 u2.setPassword("222222"); 41 u2.setSex("F"); 42 u2.setUsername("kobe"); 43 userList.add(u2); 44 UserInfo u3 = new UserInfo(); 45 u3.setId(3); 46 u3.setEmail("[email protected]"); 47 u3.setAge(33); 48 u3.setPassword("333"); 49 u3.setSex("M"); 50 u3.setUsername("kg"); 51 userList.add(u3); 52 model.addAttribute("userList", userList); 53 return "fmk"; 54 } 55 56 }View Code
1 /** 2 * 3 */ 4 package com.allen.springboot.learn.entity; 5 6 /** 7 * @author admin 8 * 9 */ 10 public class UserInfo { 11 12 private Integer id; 13 private String username; 14 private String password; 15 private String sex; 16 private String email; 17 private int age; 18 19 public Integer getId() { 20 return id; 21 } 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 public String getUsername() { 26 return username; 27 } 28 public void setUsername(String username) { 29 this.username = username; 30 } 31 public String getPassword() { 32 return password; 33 } 34 public void setPassword(String password) { 35 this.password = password; 36 } 37 public String getSex() { 38 return sex; 39 } 40 public void setSex(String sex) { 41 this.sex = sex; 42 } 43 public String getEmail() { 44 return email; 45 } 46 public void setEmail(String email) { 47 this.email = email; 48 } 49 public int getAge() { 50 return age; 51 } 52 public void setAge(int age) { 53 this.age = age; 54 } 55 56 }View Code
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Insert title here</title> 5 </head> 6 <body> 7 用戶信息<br/> 8 id:${u1.id}<br/> 9 username:${u1.username}<br/> 10 password:${u1.password}<br/> 11 sex:${u1.sex}<br/> 12 email:${u1.email}<br/> 13 age:${u1.age} 14 <hr/> 15 列表信息<br/> 16 <table> 17 <#if (userList ? size > 0 )> 18 <#list userList as user> 19 <tr> 20 <td>${user.id}</td> 21 <td>${user.username}</td> 22 <td>${user.password}</td> 23 <td>${user.sex}</td> 24 <td>${user.email}</td> 25 <td>${user.age}</td> 26 </tr> 27 </#list> 28 <#else> 29 沒有數據 30 </#if> 31 </table> 32 33 </body> 34 </html>View Code
現在就可以啟動工程,訪問controller了,信息正常信息
至此在springboot中使用freemarker已完成。
接下來看看使用thymeleaf模板,thymeleaf模板是springboot默認使用的模板,模板文件默認路徑是在src/main/resources/templates下,和上文一樣,只需要在pom.xml文件中引入對應的jar。
創建模板:
1 <!DOCTYPE html> 2 <html xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org"> 3 <head> 4 <meta charset="UTF-8" /> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 hello <span style="color:red" th:text="${name}"></span> 9 </body> 10 </html>View Code
創建controller:
1 /** 2 * 3 */ 4 package com.allen.springboot.learn.controller; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 /** 11 * @author admin 12 * 13 */ 14 @Controller 15 @RequestMapping("/thymeleaf") 16 public class ThymeleafController { 17 18 @RequestMapping("/hello") 19 public String helloThymeleaf(Model model){ 20 model.addAttribute("name", "Thymeleaf"); 21 return "tmf"; 22 } 23 24 }View Code
接下來啟動項目,訪問controller中的地址,瀏覽器端顯示如下內容
springboot入門_模板