其他分享
首页 > 其他分享> > SpringBoot整合Freemarker

SpringBoot整合Freemarker

作者:互联网

创建新项目

 

 

 

 

 

 在pom,xml文件添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>gongspringfreemarker</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gongspringfreemarker</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- freemarker启动器的坐标 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

 

 接下来大打开Hbuilder这个软件

 

 

 

 打开项目所在位置

 

 

 

 

 

 新建userList.ftl文件

 

 userList.ftl

<html>
    <head>
        <title>展示用户数据</title>
        <meta charset="utf-9"></meta>
    </head>
    
    <body>
        
        <table border="1" align="center" width="50%">
            
            <tr>
                
                <th>ID</th>
                <th>Name</th>
                <th>Age</th>
            </tr>
            
            <#list list as user >
                <tr>
                    <td>${user.userid}</td>
                    <td>${user.username}</td>
                    <td>${user.userage}</td>
                </tr>
            </#list>    
        </table>
    </body>
</html>

 

 

 创建Controller

package com.gongspringfreemarker.controller;

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

import com.gongspringfreemarker.pojo.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;



/**
 * SpringBoot整合jsp
 *
 *
 */
@Controller
public class UserController {
    /*
     * 处理请求,产生数据
     */
    @RequestMapping("/showUser")
    public String showUser(Model model){
        List<Users> list = new ArrayList<>();
        list.add(new Users(1,"张三",20));
        list.add(new Users(2,"李四",22));
        list.add(new Users(3,"王五",24));
        
        //需要一个Model对象
        model.addAttribute("list", list);
        //跳转视图
        return "userList";
    }
}

 

 

创建Users.java类

package com.gongspringfreemarker.pojo;

public class Users {
    private Integer userid;
    private String username;
    private Integer userage;
    public Integer getUserid() {
        return userid;
    }
    public void setUserid(Integer userid) {
        this.userid = userid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Integer getUserage() {
        return userage;
    }
    public void setUserage(Integer userage) {
        this.userage = userage;
    }
    public Users(Integer userid, String username, Integer userage) {
        super();
        this.userid = userid;
        this.username = username;
        this.userage = userage;
    }
    public Users() {
        super();
        // TODO Auto-generated constructor stub
    }
    
}

 

 

 

 创建启动器

package com.gongspringfreemarker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBoot启动类
 *
 *
 */
@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

 

 

 运行启动器

 

 

 

 在浏览器打开

 

标签:username,SpringBoot,Freemarker,boot,springframework,userage,整合,org,public
来源: https://www.cnblogs.com/braveym/p/11316356.html