springboot 整合freemarker

  1. 创建一个springboot工程,修改pom.xml文件,添加freemarker的启动类
    	<!-- 添加freemarker的启动类 -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-freemarker</artifactId>
    		</dependency>

     

  2. 编写模板页面user.ftl文件(必须是以.ftl结尾的文件)文件需要放到src/main/resources 目录下必 须要一个名称为 templates 

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>显示用户数据</title>
    </head>
    <body>
    	<table border="1" align="center" width="50%">
    		<tr>
    			<th>name</th>
    			<th>age</th>
    			<th>sex</th>
    		</tr>
    		<#list list as user >
    			<tr>
    				<th>${user.name }</th>
    				<th>${user.age }</th>
    				<th>${user.sex }</th>
    			</tr>
    		</#list>
    	</table>
    </body>
    </html>

    有关freemarker的标签的使用,我将在后序的学习中为大家推出

  3. 编写Controllr类

    package com.test.controller;
    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import com.test.pojo.User;
    /**
     * 整合freemarker
     * @author 26920
     *
     */
    @Controller
    public class UserController {
    	@RequestMapping(value="/showUser")
    	public String user(Model model){
    		List<User> list=new ArrayList<User>();
    		list.add(new User("list",20,"男"));
    		list.add(new User("wanwu",22,"男"));
    		list.add(new User("zhangsan",24,"男"));
    		model.addAttribute("list", list);
    		return "user";
    	}
    }
    

    注意方法的返回字符串必须和模块文件名的前缀一样,大小写不限

  4. 编写启动类,启动项目,访问路径http://localhost:8080/showUser有关freemarker的配置只要有一步不对,就会报404

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值