springBoot整合freeMarker

功能

  • 集成freemarker

  • 使用freemarker做一个简单的界面

  • 调用bootstrap美化界面

创建项目

  • 打开软件idea,新建项目->左侧选择Spring Initializr 后选择jdk的版本。

  • 根据自己的命名规范设置Group和Artifact,并选择Type类型为project。

    9

  • 选择Springboot的版本,以及添加freemarker依赖

    10

添加依赖

在pom.xml中添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

也可以在创建项目的时候添加,因为创建项目的时候忘记添加web依赖所以在这里加上

添加配置信息

在application.properties中设置如下等配置信息

spring.freemarker.charset=UTF-8
spring.freemarker.suffix=.ftl
spring.freemarker.content-type=text/html; charset=utf-8
spring.freemarker.template-loader-path=classpath:/templates

创建模板文件( .ftl)

创建index.ftl

在resources下新建一个templates目录在该目录下新建index.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
<table class="table">
    <caption>用户基本信息</caption>
    <thead>
    <tr>
        <th>id</th>
        <th>姓名</th>
    </tr>
    </thead>
    <#list userList as user>
        <tr>
            <td>${user.id!}</td>
            <td>${user.name!}</td>
        </tr>
    </#list>
</table>
</body>
</html>

创建User.java

创建entity子包,在entity下创建User.java

package indi.xzw.freemarker.entity;

public class User
{
    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

创建UserController.java

创建controller子包,在该子包下新建UserController.java

package indi.xzw.freemarker.controller;

import indi.xzw.freemarker.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping(value = "/user")
class IndexController {


    @RequestMapping(value = "/show")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("index");
        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "zhangli"));
        userList.add(new User(2, "ali"));
        userList.add(new User(3, "xiaoli"));
        modelAndView.addObject("userList", userList);
        return modelAndView;
    }

}

访问

访问:http://localhost:8080/user/show

12

添加bootstrap

修改index.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <title>Spring Boot Demo - FreeMarker</title>
    <link rel="stylesheet" href="/css/bootstrap.min.3.3.5.css"/>
    <script src="/js/jquery-1.11.2.min.js" type="text/javascript"></script>
    <script src="/js/bootstrap.js" type="text/javascript"></script>
</head>
<body>
    <table class="table">
        <caption>用户基本信息</caption>
        <thead>
        <tr>
            <th>id</th>
            <th>姓名</th>
        </tr>
        </thead>
           <#list userList as user>
            <tr>
                <td>${user.id!}</td>
                <td>${user.name!}</td>
            </tr>
           </#list>
    </table>
</body>
</html>

13

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×