Springboot + Freemarker(一)
Maven pom文件配置
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.1.5.RELEASE</version> 5 <relativePath/> <!-- lookup parent from repository --> 6 </parent> 7 8 <properties> 9 <java.version>1.8</java.version> 10 </properties> 11 12 <dependencies> 13 <dependency> 14 <groupId>org.springframework.boot</groupId> 15 <artifactId>spring-boot-starter-freemarker</artifactId> 16 </dependency> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-web</artifactId> 20 </dependency> 21 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-test</artifactId> 25 <scope>test</scope> 26 </dependency> 27 </dependencies> 28 29 <build> 30 <plugins> 31 <plugin> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-maven-plugin</artifactId> 34 </plugin> 35 </plugins>
Freemarker 页面目录配置
1 spring: 2 mvc: 3 view: 4 prefix: /templates/ 5 suffix: .ftl
Controller 编写
1 @Controller 2 @RequestMapping("test") 3 public class DemoController { 4 5 6 @RequestMapping("toDemo") 7 public String toDemo(ModelMap modelMap){ 8 List arr = new ArrayList(); 9 arr.add("狗贼"); 10 arr.add("企鹅"); 11 12 modelMap.put("arr",arr); 13 14 return "demo"; 15 } 16 17 }
ftl 页面
1 <table> 2 <tr> 3 <td>姓名</td> 4 </tr> 5 <#list arr as i> 6 <tr> 7 <td>${i}</td> 8 </tr> 9 </#list> 10 </table>
输出结果: