SpringBoot学习8:springboot整合freemarker

1、创建maven项目,添加pom依赖

<!--springboot项目依赖的父项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencies>
        <!--注入springboot启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--注入springboot对freemarker视图技术的支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
    </dependencies>

2、创建controller

package com.bjsxt.controller;

import com.bjsxt.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
 * Created by Administrator on 2019/2/6.
 */
@Controller
public class UserController {

    @RequestMapping("/toUserList")
    public String toUserList(Model model){
        List<User> userList=new ArrayList<User>();
        userList.add(new User(1L,"张三","男"));
        userList.add(new User(2L,"李四","女"));
        userList.add(new User(3L,"王五","男"));
        model.addAttribute("userList",userList);
        return "user_list";
    }
}

3、创建freemarker模版文件user_list.ftl

注意:springboot 要求模板形式的视图层技术的文件必须要放到 src/main/resources 目录下必
须要一个名称为 templates

<html>
<head>
    <title>用户列表</title>
</head>
<body>

<table border="1px solid red">
    <tr>
        <th>id</th>
        <th>姓名</th>
        <th>性别</th>
    </tr>
    <#list userList as user>
        <tr>
            <td>${user.id}</td>
            <td>${user.name}</td>
            <td>${user.sex}</td>
        </tr>
    </#list>
</table>
</body>
</html>

4、创建启动器

package com.bjsxt;

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

/**
 * Created by Administrator on 2019/2/6.
 */
@SpringBootApplication
public class App {

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

原文地址:https://www.cnblogs.com/duanrantao/p/10353866.html

时间: 2024-08-29 21:15:13

SpringBoot学习8:springboot整合freemarker的相关文章

SpringBoot学习- 8、整合Shiro

Shiro是什么,引自百度百科:Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序. 关于Shiro网上讲的很多,以下代码是来自网上几篇博客文章的代码集成, 下面是集成步骤 1.pom.xml添加以下内容 <dependency> <groupId>org.apache.shiro</groupId> <

springboot学习笔记-3 整合redis&amp;mongodb

一.整合redis 1.1 建立实体类 @Entity @Table(name="user") public class User implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String name; @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") private

SpringBoot(二)-- 整合FreeMarker模板

1.pom依赖 <!-- 引入freeMarker的依赖包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> 2.配置application.properties spring.freemarker.allow-

SpringBoot学习- 4、整合JWT

1.Json web token(JWT)是为了网络应用环境间传递声明而执行的一种基于JSON的开发标准(RFC 7519),该token被设计为紧凑且安全的,特别适用于分布式站点的单点登陆(SSO)场景.JWT的声明一般被用来在身份提供者和服务提供者间传递被认证的用户身份信息,以便于从资源服务器获取资源,也可以增加一些额外的其它业务逻辑所必须的声明信息,该token也可直接被用于认证,也可被加密. 2.pom.xml增加如下代码来添加JWT依赖包 <!-- https://mvnreposit

SpringBoot学习之SpringBoot执行器

在以往的分布式开发当中,各个服务节点的监控必不可少.监控包含有很多方面,比如说:内存占用情况,节点是否健康等.在spring-boot会给我们提供相关资源监控叫做spring-boot-actuator, 通过执行器可以帮我管理和监控生产环境下的应用服务. 一.添加SpringBoot执行器的依赖 添加gradle配置依赖: dependencies { compile('org.springframework.boot:spring-boot-starter-actuator') } 二.关于

SpringBoot学习(四)--&gt;SpringBoot快速入门,开山篇

SpringBoot是伴随着Spring4.0诞生的,旨在简化开发. SpringBoot官方文档:http://spring.io/projects/spring-boot 写个示例:Hello SpringBoot 1.创建Maven工程 工程结构如下: 2.配置pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchem

springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resources  /META-INF/resources 可以在src/main/resources目录下创建static,在该位置放置一个图片文件. 启动程序后,尝试访问http://localhost:8080/D.JPG,如能显示图片,配置成功. 2.5 SpringBoot整合freemarker视图

springboot学习笔记-5 springboot整合shiro

http://www.cnblogs.com/hlhdidi/p/6376457.html 亲自验证,该帖真实有效 shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/  它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和shiro整合的功能!接下来就用springboot结合springmvc,mybatis,整合shiro完成对于用户登录的判定和权限的验证. 1.准备数据库表结构 这里主要涉及到五张表:

springboot整合freemarker

前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配置文件修改: 3.在templates下面编写后缀为ftl的页面: 4.错误出现:404问题: (1)检查是@RestController还是@Controller,如果要返回页面必须用@Controller (2)这次问题出现的很粗心:(漏掉一个小点) 原文地址:https://www.cnblo

SpringBoot(二十六)整合Redis之共享Session

集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现(通过ip绑定服务器其中一台,就没有集群概念了):2.通过cookie备份session实现(因为cookie数据保存在客户端,不推荐;3.通过redis备份session实现(推荐); 学习本章节之前,建议依次阅读以下文章,更好的串联全文内容,如已掌握以下列出知识点,请跳过: SpringBoot(